如何以编程方式获取 DAO 的 PlatformTransactionManager?
我正在使用 wicket 开发一个通用的 CRUD 应用程序,它可以使用 AbstractDao 模式的实现来编辑任何 Spring/JPA 实体,例如 UserDaoImpl 、 ForumDaoImpl ...等
在我的 wicket 页面中: 我必须使用 @SpringBean 来识别要使用哪个 PlatformTransactionManager
,例如:
@SpringBean(name="transactionManagerUser")
private PlatformTransactionManager transactionManagerUser;
private TransactionTemplate txTemplate;
public CrudPage(final PageParameters pps , final AbstractDao<T> dao)
{
super(pps);
txTemplate= new TransactionTemplate(transactionManagerUser);
}
问题是,这些 DAO 使用不同的事务管理器,例如 transactionManagerUser
、 transactionManagerForum
...等等。
我无法在代码中硬编码这些 txManager 的名称。 我必须以编程方式获取 dao 的 txManager 。如何实现这一目标?
多谢 !
------- 更新 -------
好吧,我通过将 PlatformTransactionManager
传递到 CRUD 页面解决了这个问题(并使用 transactionTemplate = new TransactionTemplate(platformTransactionManager)
生成一个txTemplate',效果很好,虽然不能直接解决问题,但无论如何,它是有效的。
I am developing a generic CRUD app with wicket , that can edit any Spring/JPA entities with a AbstractDao pattern's implementation , such as UserDaoImpl , ForumDaoImpl ...etc
In my wicket page :
I have to use @SpringBean to identify which PlatformTransactionManager
to use , such as :
@SpringBean(name="transactionManagerUser")
private PlatformTransactionManager transactionManagerUser;
private TransactionTemplate txTemplate;
public CrudPage(final PageParameters pps , final AbstractDao<T> dao)
{
super(pps);
txTemplate= new TransactionTemplate(transactionManagerUser);
}
The problem is , these DAOs are using different transaction managers , such as transactionManagerUser
, transactionManagerForum
...etc.
I cannot hard-code these txManager's name in my code.
I have to programmatically get the dao's txManager . How to achieve that ?
Thanks a lot !
------- updated -------
Well , I've solved this problem by passing PlatformTransactionManager
to the CRUD page ( and use transactionTemplate = new TransactionTemplate(platformTransactionManager)
to generate a txTemplate ' , and it works well. Although not directly solve the problem , anyway , it works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为什么认为应该将事务逻辑放入代码中? Spring 使用方面以声明方式管理事务,因此您不必这样做。如果你认为你需要这样做,那你就错了。
交易不应该是 DAO 的职责范围;服务拥有并管理事务,因为可能有多个 DAO 参与单个事务。
我想知道为什么你需要几个事务管理器。如果您有多个数据库,则应该为每个数据库使用 XA 驱动程序,并使用单个事务管理器来处理两阶段提交。
And why do you think you should be putting transaction logic in your code? Spring uses aspects to manage transactions declaratively so you don't have to. If you think you need to, you're doing it wrong.
Transactions should not be the province of the DAOs; services own and manage transactions, because there might be several DAOs participating in a single transaction.
I wonder why you need several transaction managers. If you have several databases, you should be using XA drivers for each one and a single transaction manager to handle two-phase commit.