Spring 3、TransactionManagement、Tomcat 和更改数据源中途
我有一个用 @Transactional 注释进行注释的方法。
问题是,代码运行所针对的数据源可能会改变!简而言之,是否可以在应用程序运行时更改事务的数据源?
深入: 该应用程序允许用户选择要运行 sql 的数据库。他们可以在运行时更改数据库。我希望应用程序在一个事务中运行所有 sql - sql 组始终针对同一数据源,因此这不是问题。
问题是,我不知道如何更改事务以使用不同的数据源。不同的文章建议使用 JTATransactionManager,但我们使用的是 tomcat,所以没有骰子。
有没有一种简单的方法可以做到这一点,或者我们需要放弃 @Transactional 注释并做其他事情?
如果是这样,那“其他东西”是什么?
哦,数据库是db2,如果有什么用的话!
谢谢!
I have a method that I've annotated with the @Transactional annotation.
The problem is, the datasource that the code runs against can change! Briefly, is it possible to change the transaction's datasource while the application is running?
In depth:
The application lets users select a database to run sql against. They can change the database at runtime. I'd like the application to run all the sql in a transaction - the sql groups are always against the same datasource, so that's not an issue.
The issue is, I don't know how to changem the transaction to use a different datasource. Varying articles have suggested the JTATransactionManager, but we're on tomcat, so no dice.
Is there a simple way to do this, or will we need to ditch the @Transactional annotation and do something else?
And if so, what is that "something else?"
Oh, the database is db2, if that's any use!
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能的解决方案之一
在spring config中配置所有可能的数据源
a. DB1 的数据源 1
b. Datasource2 for DB2 等..
在服务类中维护上述数据源的映射,并根据映射中的某些键从选定的数据源创建 spring 的 JDBCTemplate。
One of the possible solution
Configure all the possible datasources in spring config
a. Datasource1 for DB1
b. Datasource2 for DB2 etc..
Maintain a map of above datasource in a service class and create spring's JDBCTemplate out of selected datasource based on some key from map.