在下面的代码方法doService1()
更新正确的SQL,但doService2()
SQL有一些问题,但是当我打电话doService()
它必须提交doService1()
更新数据库,即使doService2()
有一个sql exception
,因为doService2()
有REQUIRES_NEW Propagation
类型,但是当我尼姑这doService1()
更新不提交DB ..
@Service public class DbClass {
static Logger log = Logger.getLogger(
DbClass.class.getName());
@Autowired
private DataSource dataSource;
@Transactional(propagation=Propagation.REQUIRED)
public void doService(){
doService1();
doService2();
}
@Transactional(propagation=Propagation.REQUIRED)
public void doService1(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = " update BATCHJOBSTATUS set PROCESSINGDATE = '20130322' " ;
int rowCount1 = jdbcTemplate.update(sql);
System.out.println(" rowCount1 >" + rowCount1);
}
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void doService2(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = " update aa set a_name = 'hhh' where a_id = 4 and " ;
int rowCount1 = jdbcTemplate.update(sql);
System.out.println(" rowCount2 >" + rowCount1);
}
}
在下面的方式很好,但仍然面临着同样的问题,你的球员的建议测试。 在这里,我doService2()
在一个单独的类,但即使仍然有同样的问题,上述
@Service
public class DbClass {
static Logger log = Logger.getLogger(
DbClass.class.getName());
@Autowired
private DataSource dataSource;
@Autowired
private DbClass2 dbClass2;
@Transactional
public void doService(){
doService1();
dbClass2.doService2();
}
@Transactional(propagation=Propagation.REQUIRED )
public void doService1(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = " update BATCHJOBSTATUS set PROCESSINGDATE = '20130322' " ;
int rowCount1 = jdbcTemplate.update(sql);
System.out.println(" rowCount1 >" + rowCount1);
}
}
@Service
public class DbClass2 {
@Autowired
private DataSource dataSource;
@Transactional(propagation=Propagation.REQUIRES_NEW)
public void doService2() {
System.out.println("*******doService2*********`");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = " update aa set a_name = 'hhh' where a_id_ = 4 " ;
int rowCount2 = jdbcTemplate.update(sql);
System.out.println(" rowCount2 >" + rowCount2);
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.spring"/>
<tx:annotation-driven transaction-manager="txManager1" proxy-target-class="true"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.8.121:1521:h3" />
<property name="username" value="admin" />
<property name="password" value="admin" />
</bean>
<bean id="txManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="batchJob" class="com.spring.jdbc.BatchJob">
</bean>
</beans>