Goal I want to introduce a transaction manage on my data source.
Looking for the correct way to use same instance of datasource for the transaction manager as well. My requirement is specify to Java Config way to pass the "Same instance" of DS to Transaction Manager. Correct me If there is a gap in my understanding.
In my case I have a datasource and of type autocommit false
, and by using the Transaction Manager specified below, I want to commit/rollback a transaction (e.g. Update an operation/Revert an Update operation ---when there a error/no error in the transaction).
However, while debugging I have noticed that when I used java config specified below, I get two different instance of data source and trx.commit()
does not work.
Programmatic transaction management (https://docs.spring.io/spring/docs/3.0.0.M4/reference/html/ch10s06.html)
@Bean
public DataSource dataSource() {
return getMyDataSource(); //new instance of datasource.//this datasource is autocommit-false
}
@Bean
public DataSourceTransactionManage trxManager() {
return getTransationManage(dataSource()); // this creates another instance of dataSource
}
Any help in this regard is highly appreciated.
Edit :- I was using Mybatis with Spring. Basically, I had to configure the DataSouce correctly. Below links were useful.
[Pass parameters dynamically to Spring beans ][1] [Mybatis Transaction Management CTM and PTM ][2] [Spring Transaction Management Notes ][3] [Spring & JTA NOtes][4]
[1]: https://stackoverflow.com/a/21202458/5086633
[2]: http://www.mybatis.org/spring/transactions.html
[3]: https://docs.spring.io/spring/docs/3.0.0.M4/reference/html/ch10s06.html
[4]: https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access.html#transaction
To use back the same instance of dataSource
how about you do this:
@Bean
@Autowired
public DataSourceTransactionManage trxManager(DataSource dataSource) {
return getTransationManage(dataSource);
}