简体   繁体   English

春季交易管理

[英]Spring transaction management

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. 我正在研究spring4 mvc,以在我们的新Web应用程序中进行介绍,目前,我们正在使用struts1.x,并希望使用新框架尽可能简单地支持html5 / ajax请求,并希望使用DI和spring webflow支持的强大功能。

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. 当前,在struts1.x应用程序中,db事务管理是在自定义的GenericAction(它是Action的子类)上完成的,在GenericAction中,我们正在从数据源获取连接并切换到子类,然后引发任何异常,然后捕获并回滚,否则提交,数据库原子性(事务管理)在一个地方完成。 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. 所有模块动作类都应该扩展GenericAction,以便数据库连接可用并执行与模块相关的工作,并且在完成连接后,可以如上所述在GenericAction中回滚或提交。

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. 在春季,事务的作用域以@Transactional注释开始,然后以Service Class中的方法结束,因为该服务类标记为@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 我正在使用HibernateTransactionManager作为事务管理器

  1. Transaction should start from interceptors in case web request or any class (in case of unit testing). 如果Web请求或任何类(在单元测试的情况下),则事务应从拦截器开始。

  2. Transaction should ends with the after interceptor is executed in case of web request or any class in case of unit testing. 对于Web请求,事务应在执行拦截器后进行;对于单元测试,事务应在执行任何类后结束。

  3. In case of any exception raised then our HandlerExceptionResolverImpl handler will execute then connection should be rollback. 如果引发任何异常,则我们的HandlerExceptionResolverImpl处理程序将执行,则连接应回滚。

Any workaround or best practices would be greatly appreciate. 任何解决方法或最佳做法将不胜感激。

Thanks Dhorrairaajj 感谢Dhorrairaajj

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). 我认为,可以通过遵循Spring在org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(Object,TransactionDefinition)中所做的操作,在当前应用程序上进行最小的更改来实现。 In detail, I think something like following should work: 详细地说,我认为类似以下的方法应该起作用:

  1. On the GenericAction, get connection from data source (as you've already done) 在GenericAction上,从数据源获取连接(如已完成)
  2. Bind the connection via its holder with the current data source: TransactionSynchronizationManager.bindResource(getDataSource(), txObject.getConnectionHolder()); 通过其持有者将连接与当前数据源绑定:TransactionSynchronizationManager.bindResource(getDataSource(),txObject.getConnectionHolder());
  3. Eventually, commit the transaction as org.springframework.jdbc.datasource.DataSourceTransactionManager.doCommit(DefaultTransactionStatus). 最后,将事务提交为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. 无论如何,Spring的http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/support/TransactionTemplate.html可以帮助实现您想要的任何程序化事务方案。 Just need to ensure that the right transactional resource is bound to the opening transaction. 只需确保将正确的交易资源绑定到期初交易即可。

Hope this helps. 希望这可以帮助。

Thanks for you reply. 感谢您的回复。 Your post/suggestion drives to do the following 您的帖子/建议促使您执行以下操作

I written an interceptor called TxtManager and below are methods to perform the transaction management. 我编写了一个名为TxtManager的拦截器,以下是执行事务管理的方法。

public boolean preHandle(HttpServletRequest request,
    HttpServletResponse response, Object handler) throws Exception
{
    Session session = sessionFactory.openSession();
    SessionHolder hold = new SessionHolder(
        session);
    TransactionSynchronizationManager.bindResource(sessionFactory, hold);
    // passing null would accept the default transaction definition.
    status = transactionManager.getTransaction(null);

    return true;
}


public void afterCompletion(HttpServletRequest request,
    HttpServletResponse response, Object handler, Exception ex)
    throws Exception
{
    Exception hanlderEx = (Exception) request
        .getAttribute(HandlerExceptionResolverImpl.EXCEPTION_KEY);

    if (hanlderEx != null)
    {
        transactionManager.rollback(status);
    }
    else
    {
        transactionManager.commit(status);
    }
}

In HandlerExceptionResolverImpl class which responsible for handling things upon exception, put an exception object in request and read the same exception to rollback or commit in afterCompletion method of the interceptor. 在负责处理异常情况的HandlerExceptionResolverImpl类中,将异常对象放入请求中,并读取相同的异常以回滚或在拦截器的afterCompletion方法中提交。

In between(preHandler and afterCompletion) we will going to use the standard practices to perform the module related stuff including any other interceptos. 在之间(preHandler和afterCompletion)中,我们将使用标准实践来执行与模块相关的工作,包括任何其他拦截器。 Unit testing may not be supported, i will check other alternative for unit testing. 可能不支持单元测试,我将检查其他替代单元测试。

Finally we were able to simulate the existing frame work with your suggestion Thanks 最后,我们能够根据您的建议模拟现有框架

Any suggestion to improves this would be much appreciate !!.. 任何建议,以改善这一点将不胜感激!! ..

Thanks Dhorrairaajj 感谢Dhorrairaajj

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM