简体   繁体   English

Spring HibernateDaoSupport创建多个会话

[英]Spring HibernateDaoSupport creates multiple sessions

I am using Struts2.3 + Spring 3.2.6 + Hibernate 3.X for my web application. 我正在为我的Web应用程序使用Struts2.3 + Spring 3.2.6 + Hibernate3.X。

I am using annotations to manage the transaction. 我正在使用注释来管理交易。 My DAO class is as below. 我的DAO课程如下。

@Transactional(readOnly = true, rollbackFor={Exception.class})
public class CustomerDAOImpl extends HibernateDaoSupport implements CustomerDAO{

    //adds the customer
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void addCustomer(Customer customer){
        Session session = getSession();
        System.out.println("M1() Hash Code: --->"+session.hashCode()+ " Thread id: "+Thread.currentThread().getId());
        //session.save(customer);
    }

    //return all the customers in list
    // @Transactional(propagation=Propagation.REQUIRED, rollbackFor = {Exception.class})
    @Transactional(readOnly = true)
    public List<Customer> listCustomer(){
        Session session = getSession();
        System.out.println("M2() Hash Code: --->"+session.hashCode()+ " Thread id: "+Thread.currentThread().getId());
        return null; //logic to get the list based on condition

    }

These methods will be called from service layer and it is like below; 这些方法将从服务层调用,如下所示;

customerDAO.addCustomer(customer);
customerDAO.listCustomer();

I am getting different sessions for same thread when the above code is executed. 当执行以上代码时,我在同一线程获得不同的会话

Output: 输出:

M1() Hash Code: --->5026724 Thread id: 21
M2() Hash Code: --->8899550 Thread id: 21

Due to this, if any exception comes in method2() the data which is persisted using method1() is not rollback . 因此, 如果 method2()中出现任何异常 ,则使用method1()保留的数据不会回滚

Please let me know, how to get the single session based on thread (if i go for my own HibernateFactory i lose transaction management by Spring) with out loosing spring transaction management. 请让我知道,如何在不丢失Spring事务管理的情况下如何基于线程获取单个会话(如果我去自己的HibernateFactory,那么到Spring之前我将丢失事务管理)。

If you are using Hibernate make your DAO class to extend HibernateDaoSupport. 如果您使用的是Hibernate,请创建DAO类以扩展HibernateDaoSupport。 Now you can get session from getHibernateTemplate() method. 现在,您可以从getHibernateTemplate()方法获取会话。 That way you can have the session managed by spring-hibernate 这样,您可以将会话由spring-hibernate管理

You can try this - 你可以试试这个-

appContext.xml appContext.xml

<tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
         <tx:method name="submit*" propagation="REQUIRED" read-only="false" rollback-for="Exception"/>
</tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut id="appControllerTransactionPointCuts"
            expression="execution(* com.test.customer.bo.Custom.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="appControllerTransactionPointCuts" />
    </aop:config>

Remove - 去掉 -

<tx:annotation-driven transaction-manager="transactionManager" />

from appcontext.xml 来自appcontext.xml

Now AOP should manage transactions. 现在,AOP应该管理事务。

How Transactions work - Suppose you are persisting objects in multiple DAO methods and you want them all to happen in one transaction, then you have to apply transaction at the service layer method where the DAO methods are getting called. 事务如何工作-假设您要在多个DAO方法中持久保存对象,并且希望它们全部在一个事务中发生,那么您必须在调用DAO方法的服务层方法中应用事务。 For e., in my case the service layer is com.test.customer.bo.Custom.* 例如,在我的情况下,服务层是com.test.customer.bo.Custom。*

If you are not doing so, then each of your DAO method will be executed in a separate transaction and will get persisted to database if no exception occurs. 如果您不这样做,那么每个DAO方法都将在单独的事务中执行,并且如果不发生异常,则将其持久化到数据库中。 If exception occur in method2() of DAO layer it will not rollback method1() as it is already committed. 如果DAO层的method2()中发生异常,则不会回滚method1(),因为它已经提交。

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

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