Hibernate spring annotation sessions not being closed/flushed

后端 未结 4 1818
情书的邮戳
情书的邮戳 2021-01-07 08:04

I\'ve \'inherited\' a project which uses Spring annotations to manage transactions/sessions with Hibernate. Or at least it\'s meant to be. Currently the Hibernate sessions n

相关标签:
4条回答
  • 2021-01-07 08:26

    using hibernate template solves it

    @Autowired
    public void setSessionFactory(SessionFactory sessionFactory) {
           hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
    

    and then use the template like in this link http://singgihpraditya.wordpress.com/2010/02/13/spring-3-0-and-hibernate-tutorial-part-1/

    your reply would be great to share

    0 讨论(0)
  • 2021-01-07 08:38

    One of my cleverer colleagues discovered what the problem was.

    The actual problem was that the method that you've declared as @Transactional is an inherited method that is called from a base class, which means that Spring is unable to intercept calls to the method and wrap it in a transaction.

    Spring implements transaction management as aspects, and aspects are implemented with proxies. The limitation of this is that if an object calls a method on itself (which is what's happening here because of inheritance) then the proxy doesn't see the call (because it happens internally within the class, like calling a private method), and can't so anything about it.

    Which makes sense but seems to be incredibly dangerous as it fails to write any data without any error message or warning.

    0 讨论(0)
  • 2021-01-07 08:39

    there is another way instead of hibernate template( hibernate template will couple spring to hibernate that is why it is deprecated in Spring 3.1 ) the key to solve this issue is to remove any configuration for the hibernate except the dialct and the url in simple way,Remove the transactional stuff from your hibernate properties as You want spring to manage transactions, not hibernate. as Marten Deinum mentioned in this url http://forum.springsource.org/archive/index.php/t-47667.html here is a snippet of my spring xml configuration file

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">    
            <value>
                classpath:/com/spring/hibernate.cfg.xml
            </value>
        </property>       
        </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    

    and here is the hibernate file

    <session-factory> 
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>
    
    <mapping resource="com/spring/BloodType.hbm.xml" />
    </session-factory>
    

    do not forget to write @Transactional in your method or class also use

    Session session = sessionFactory.getCurrentSession();
    

    and this

    BloodType b = new BloodType();
    b.setId(new Long(1));
    BloodType b1 = (BloodType)session.get(BloodType.class, 12L);
    session.delete(b1);
    session.save(b);
    

    would work just fine without any need for session.flush() nor transaction.commit() hope it works

    0 讨论(0)
  • 2021-01-07 08:46

    if the transaction (which is annotated) succeeds then transaction.commit() is called automatically which will write the contents to the DB, please check if the transaction is being called and there are no exceptions being thrown.

    also ensure that you link your sessionfactory to the transcation which is declared.

    0 讨论(0)
提交回复
热议问题