Spring에서 두 개의 데이터 소스를 사용하는 애플리케이션을 개발해야한다. 하나의 데이터 소스에서 몇 가지 예외가 발생하면 다른 데이터 소스도 롤백해야하므로 두 데이터 소스는 트랜잭션에 의해 관리되어야합니다. 내 이력은 현재 jta를 사용한 현재의 임플란트가 작동하지 않는다는 것입니다. 코드를 테스트 할 때 한 데이터 소스에 오류가있는 경우 다른 데이터 소스가 롤백되지 않았습니다. 그냥 커밋되었습니다.Spring에서 다중 데이터 소스를 가진 JTA 트랜잭션
<bean id="parentDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
abstract="true">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
</bean>
<bean id="firstDataSource" parent="parentDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
</bean>
<bean id="secondDataSource" parent="parentDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/test2"/>
</bean>
<bean id="jtaTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
</bean>
<bean id="customerDAO" class="springapp.datasource.CustomerDAO">
<property name="dataSource">
<ref local="firstDataSource"/>
</property>
<property name="dataSource2">
<ref local="secondDataSource"/>
</property>
<property name="jtaTransactionManager">
<ref local="jtaTransactionManager"/>
</property>
</bean>
customerDAO의 트랜잭션 코드는 다음과 같습니다 :
public class CustomerDAO {
private DataSource dataSource;
private DataSource dataSource2;
private JtaTransactionManager jtaTransactionManager;
public DataSource getDataSource2() {
return dataSource2;
}
public void setDataSource2(DataSource dataSource2) {
this.dataSource2 = dataSource2;
}
public JtaTransactionManager getJtaTransactionManager() {
return jtaTransactionManager;
}
public void setJtaTransactionManager(JtaTransactionManager jtaTransactionManager) {
this.jtaTransactionManager = jtaTransactionManager;
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void insertItem(Item item){
TransactionTemplate tt = new TransactionTemplate(jtaTransactionManager);
tt.execute(new TransactionCallback<Object>(){
@Override
public Object doInTransaction(TransactionStatus arg0) {
JdbcTemplate jt = new JdbcTemplate(dataSource);
String sql = "insert into item(name,price) values ('aaaa',11);";
jt.setDataSource(dataSource);
jt.update(sql);
sql = "insert into item(name,price) values ('aaaa',12);";
jt.setDataSource(dataSource2);
jt.update(sql);
return null;
}
});
}
}
사람은 문제가 어디 말해 줄 수 여기 내 구성 파일입니다?