Spring 3、TransactionManagement、Tomcat 和更改数据源中途

发布于 2024-12-23 08:43:01 字数 405 浏览 2 评论 0原文

我有一个用 @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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺深海 2024-12-30 08:43:01

可能的解决方案之一

  1. 在spring config中配置所有可能的数据源

    a. DB1 的数据源 1
    b. Datasource2 for DB2 等..

  2. 在服务类中维护上述数据源的映射,并根据映射中的某些键从选定的数据源创建 spring 的 JDBCTemplate。

One of the possible solution

  1. Configure all the possible datasources in spring config

    a. Datasource1 for DB1
    b. Datasource2 for DB2 etc..

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文