0

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
yeppe
  • 679
  • 1
  • 11
  • 43
  • Is the class that you copied this code from annotated with @Configuration? T confirm, could you also include both the class definition, and where you create the context? – racraman Nov 22 '18 at 04:32
  • Yes. This form of declaring `@Bean` dependencies only happens in `@Configuration` annotated classes (as shown in https://docs.spring.io/spring/docs/3.0.0.M4/reference/html/ch03s11.html#beans-javaconfig-configuration-annotation ). See also https://stackoverflow.com/questions/40256702/spring-bean-can-still-work-without-configuration – racraman Nov 22 '18 at 23:15

1 Answers1

1

To use back the same instance of dataSource how about you do this:

@Bean
@Autowired
public DataSourceTransactionManage trxManager(DataSource dataSource) {
    return getTransationManage(dataSource);
}
Mamun Sardar
  • 2,679
  • 4
  • 36
  • 44
  • You don't need to pass anything. Spring will inject the bean in your param. And yes, you need @Configuration annotation on the class. Sample: https://pastebin.com/cS7uutWr – Mamun Sardar Nov 22 '18 at 06:04
  • If you are facing `multiple bean found of dataSource` rename the above dataSource bean with `@Bean(name = "myDataSource")` and change the param name accordingly `DataSource myDataSource` – Mamun Sardar Nov 22 '18 at 06:33