Announcement Announcement Module
Collapse
No announcement yet.
Hibernate Transaction best practice Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate Transaction best practice

    Is there a better way to fix the error that the way its done below.

    I got the following exception
    Code:
    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    For this I changed the following code
    Code:
    @SuppressWarnings("unchecked")
    @RemotingInclude
    @Transactional
    public List<AppRoleHibernate> findAll() {
      List<AppRoleHibernate> t = null;
      try {
    	t = sessionFactory.getCurrentSession().createQuery("from AppRoleHibernate").list();
      } catch (Exception e) {
     	e.printStackTrace();
      }
      return t;
    }
    to

    Code:
    @SuppressWarnings("unchecked")
    @RemotingInclude
    @Transactional
    public List<AppRoleHibernate> findAll() {
      List<AppRoleHibernate> t = null;
      try {
      	Session session = sessionFactory.openSession();
      	t = session.createQuery("from AppRoleHibernate").list();
    AppRoleHibernate").list();
      } catch (Exception e) {
     	e.printStackTrace();
      }
      return t;
    }
    I have wired the session factory as follows -
    Code:
    private SessionFactory sessionFactory;
    	
    @Autowired
    public void setSessionFactory(SessionFactory factory) {
    	sessionFactory = factory;
    }
    <bean id="sessionFactory"
    	class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    	<property name="dataSource" ref="dataSource2" />
    	<property name="packagesToScan" value="flex.spring.samples" />
    	<property name="hibernateProperties">
    	<props>
    		<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
    		<!-- <prop key="hibernate.hbm2ddl.auto">create</prop>  -->
    	</props>
    	</property>
    </bean>
    I though that making the method @Transactional would openSession for me but I guessed wrong. Is there a way to configure this rather than add it to each method ?
    Last edited by Java Developer; Oct 13th, 2010, 02:46 AM.

  • #2
    If you want to run out of resources that is the way to go... bsaically NEVER use openSession!!! that always creates a new session outside the scope and management of spring! Which will leave you with open connections until they time out and eventually your database will stop/crash under the load or your connection pool starves.

    1) Use tx:annotation-driven
    2) proxy target classes you have multiple annotations and they need to be retained
    3) Don't catch exceptions, that ruins proper tx management, catch and rethrow ok but don't swallow

    Comment


    • #3
      I understood the part about Exception handling and I am trying to setup Spring to do that for me. Also, got the part about why not to use openSession but didn't understand the alternative options that you had mentioned.

      1) Use tx:annotation-driven
      2) proxy target classes you have multiple annotations and they need to be retained
      Can you elaborate.

      Comment


      • #4
        Only adding @Transactional is going to do nothing, you need something that knows what to do with @Transaction, the tx:annotation-driven in your application context. As you use multiple annotations you want to make sure you use classproxies to retrain those annotations.

        Comment


        • #5
          Thanks.... That's what makes Spring simple I guess. All I had to do was to add the following and I could remove the openSession.

          Code:
          xmlns:tx="http://www.springframework.org/schema/tx"
          xsi:schemaLocation="
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
          <context:component-scan base-package="flex.spring.samples" />
          <tx:annotation-driven />
          Now how do I
          proxy target classes you have multiple annotations

          Comment

          Working...
          X