Spring Transaction Manager and DataSource Java Config way to pass same bean id without creating a new...












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









share|improve this question

























  • 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 4:32











  • Yes. This form of declaring @Bean dependencies only happens in @Configuration annotated classes (as shown in docs.spring.io/spring/docs/3.0.0.M4/reference/html/… ). See also stackoverflow.com/questions/40256702/…

    – racraman
    Nov 22 '18 at 23:15


















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









share|improve this question

























  • 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 4:32











  • Yes. This form of declaring @Bean dependencies only happens in @Configuration annotated classes (as shown in docs.spring.io/spring/docs/3.0.0.M4/reference/html/… ). See also stackoverflow.com/questions/40256702/…

    – racraman
    Nov 22 '18 at 23:15
















0












0








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









share|improve this question
















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






java spring datasource spring-transactions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 4:29







yeppe

















asked Nov 22 '18 at 3:37









yeppeyeppe

3051526




3051526













  • 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 4:32











  • Yes. This form of declaring @Bean dependencies only happens in @Configuration annotated classes (as shown in docs.spring.io/spring/docs/3.0.0.M4/reference/html/… ). See also stackoverflow.com/questions/40256702/…

    – racraman
    Nov 22 '18 at 23:15





















  • 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 4:32











  • Yes. This form of declaring @Bean dependencies only happens in @Configuration annotated classes (as shown in docs.spring.io/spring/docs/3.0.0.M4/reference/html/… ). See also stackoverflow.com/questions/40256702/…

    – racraman
    Nov 22 '18 at 23:15



















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 4:32





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 4:32













Yes. This form of declaring @Bean dependencies only happens in @Configuration annotated classes (as shown in docs.spring.io/spring/docs/3.0.0.M4/reference/html/… ). See also stackoverflow.com/questions/40256702/…

– racraman
Nov 22 '18 at 23:15







Yes. This form of declaring @Bean dependencies only happens in @Configuration annotated classes (as shown in docs.spring.io/spring/docs/3.0.0.M4/reference/html/… ). See also stackoverflow.com/questions/40256702/…

– racraman
Nov 22 '18 at 23:15














1 Answer
1






active

oldest

votes


















1














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



@Bean
@Autowired
public DataSourceTransactionManage trxManager(DataSource dataSource) {
return getTransationManage(dataSource);
}





share|improve this answer
























  • 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: pastebin.com/cS7uutWr

    – Mamun Sardar
    Nov 22 '18 at 6: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 6:33












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423530%2fspring-transaction-manager-and-datasource-java-config-way-to-pass-same-bean-id-w%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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



@Bean
@Autowired
public DataSourceTransactionManage trxManager(DataSource dataSource) {
return getTransationManage(dataSource);
}





share|improve this answer
























  • 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: pastebin.com/cS7uutWr

    – Mamun Sardar
    Nov 22 '18 at 6: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 6:33
















1














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



@Bean
@Autowired
public DataSourceTransactionManage trxManager(DataSource dataSource) {
return getTransationManage(dataSource);
}





share|improve this answer
























  • 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: pastebin.com/cS7uutWr

    – Mamun Sardar
    Nov 22 '18 at 6: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 6:33














1












1








1







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



@Bean
@Autowired
public DataSourceTransactionManage trxManager(DataSource dataSource) {
return getTransationManage(dataSource);
}





share|improve this answer













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



@Bean
@Autowired
public DataSourceTransactionManage trxManager(DataSource dataSource) {
return getTransationManage(dataSource);
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 4:10









Mamun SardarMamun Sardar

1,47942441




1,47942441













  • 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: pastebin.com/cS7uutWr

    – Mamun Sardar
    Nov 22 '18 at 6: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 6:33



















  • 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: pastebin.com/cS7uutWr

    – Mamun Sardar
    Nov 22 '18 at 6: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 6:33

















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: pastebin.com/cS7uutWr

– Mamun Sardar
Nov 22 '18 at 6:04





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: pastebin.com/cS7uutWr

– Mamun Sardar
Nov 22 '18 at 6: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 6:33





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 6:33




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423530%2fspring-transaction-manager-and-datasource-java-config-way-to-pass-same-bean-id-w%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?