问题描述
我正在致力于spring4 mvc在我们的新Web应用程序中引入,目前我们正在使用struts1.x并希望使用新的框架来支持html5 / ajax请求尽可能简单,并希望使用DI和spring的强大功能webflow支持。
目前在我们的struts1.x应用程序中,db事务管理是在Action的子类GenericAction中完成的,在GenericAction中,我们从数据源并切换到子类,然后引发任何异常,然后捕获和回滚,否则提交数据库原子性(事务管理)在一个地方完成。所有的模块动作类都应该扩展GenericAction,以便数据库连接可用并执行与模块有关的东西,并且在完成连接之后,将如上所述在GenericAction中进行回滚或提交。
在春季,事务的范围以@Transactional注释开始,然后以服务类中的方法结束,因为服务类标记为@Transactional。对我来说这不是可行的解决方案。在开始之前,我已经阅读了几个与春季交易有关的文件,下面是我的问题。
我使用HibernateTransactionManager作为事务管理器
$ b
-
事务应从拦截器开始,以防Web请求或任何类单元测试的情况)。
-
在单元测试的情况下,事务应以Web请求或任何类别执行后拦截器结束。
> -
在引发任何异常的情况下,我们的HandlerExceptionResolverImpl处理程序将执行,然后连接应该回滚。
任何解决方法或最佳实践将不胜感激。
感谢
Dhorrairaajj
- 在GenericAction上,从数据源获取连接(就像您已经完成的那样)
- 通过持有者将连接绑定到当前数据源:
TransactionSynchronizationManager.bindResource(getDataSource(),txObject.getConnectionHolder());
最后,将事务提交为org.springframework.jdbc.datasource.DataSourceTransactionManager.doCommit(DefaultTransactionStatus)。
在任何情况下,Spring的可帮助您实现所需的任何编程事务方案。只需确保正确的事务资源绑定到开放事务。
希望这有助于。
I am working on spring4 mvc to introduce in our new web application and currently we are using struts1.x and wanted to use new framework for support html5/ajax request as simple as possible and wanted to use the power of DI and spring webflow support.
Currently in our struts1.x application db transaction management is done at our custom GenericAction which is subclass of Action, In GenericAction we are getting the connection from data source and handover to the subclasses then any exception raised then caught and rollback, otherwise commit such that database atomicity (transaction management )is done at one place. All the module action classes should extends GenericAction such that database connection will be available and performs the module related stuff and after completing connection will be either rollback or commit in GenericAction as said above.
In spring, scope of the Transaction is started with @Transactional annotation and then ends with a method in Service Class since the service class marked as @Transactional. This is not feasible solution for me. I have read several documents related to the spring transaction before starting and below are my questions.I am using HibernateTransactionManager as transaction manager
Transaction should start from interceptors in case web request or any class (in case of unit testing).
Transaction should ends with the after interceptor is executed in case of web request or any class in case of unit testing.
In case of any exception raised then our HandlerExceptionResolverImpl handler will execute then connection should be rollback.
Any workaround or best practices would be greatly appreciate.
ThanksDhorrairaajj
In my opinion, you can achieve this with minimal change on the current application by following what Spring does in its org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(Object, TransactionDefinition). In detail, I think something like following should work:
- On the GenericAction, get connection from data source (as you've already done)
- Bind the connection via its holder with the current data source:TransactionSynchronizationManager.bindResource(getDataSource(), txObject.getConnectionHolder());
- Eventually, commit the transaction as org.springframework.jdbc.datasource.DataSourceTransactionManager.doCommit(DefaultTransactionStatus).
In any case, Spring's http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/support/TransactionTemplate.html can help to achieve any programmatic transactional scenario that you want. Just need to ensure that the right transactional resource is bound to the opening transaction.
Hope this helps.
这篇关于春季交易管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!