Announcement Announcement Module
Collapse
No announcement yet.
Having real trouble configuring JTA txManagement w/ JBoss & Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Having real trouble configuring JTA txManagement w/ JBoss & Hibernate

    Hi everyone,

    First, please don't refer me to the forums for the answer! I've been living with the forums for a day and a half; the examples are all over the place, and none are working for me. I've also gone through the Spring reference, and two of the better Spring books and still can't nail it down.

    What I want to do should be simple. I want to use Hibernate to access two datasources using Spring. My environment is Hibernate 3.2.1, Spring 2.0.1, JBoss 4.0.5.

    I understand that I need to create two Hibernate session factories, and have them transaction managed by JTA. If anyone knows, what are the steps to configure in the Spring context config? Here's what I've got so far. The error I'm now getting is that the second session factory defined, when used, does the below:

    Session currentSession = sessionFactory2.getCurrentSession();
    - throws -
    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    Yet the first session factory works! It provides a session, and the query is successful.

    Here is my configuration (The db info. is defined in JBoss and the *hibernate.cfg.xml files):

    HTML Code:
    	<bean id="sessionFactory1"
              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="configLocation">
    			<value>/WEB-INF/domain1-hibernate.cfg.xml</value>
    		</property>
    		
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
    				<prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</prop>
    				<prop key="hibernate.current_session_context_class">jta</prop>
    				<prop key="hibernate.connection.release_mode">after_statement</prop>	
    			</props>
    		</property>
    	</bean>
    
    	<bean id="sessionFactory2"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="configLocation">
    			<value>/WEB-INF/domain2-hibernate.cfg.xml</value>
    		</property>
    		
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
    				<prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</prop>
    				<prop key="hibernate.current_session_context_class">jta</prop>
    				<prop key="hibernate.connection.release_mode">after_statement</prop>		
    			</props>
    		</property>
    	</bean>
    
    	<bean id="txManager"
    		class="org.springframework.transaction.jta.JtaTransactionManager">
    		<property name="userTransactionName">
    			<value>UserTransaction</value>
    		</property>
    	</bean>
    
    	<tx:advice id="txAdvice" transaction-manager="txManager">
    		<tx:attributes>
    			<tx:method name="*" read-only="true"/>
    		</tx:attributes>
    	</tx:advice>
    	
    	<aop:config>
    		<aop:pointcut id="myClassMethods"
    			expression="execution(* com.x.y.z.MyClass.*(..))" />
    		<aop:advisor advice-ref="txAdvice"
    			pointcut-ref="myClassMethods" />
    	</aop:config>
    Please point out what I am wrongly configuring, or not including in my configuration. Let me know if you need anything further from me. I've been working on this for two days now, and I really need the concrete configuration steps to set this up. Thanks much in advance!

    BTW, here are the logs from the point where I'm trying to obtain the current session to the error:

    2008-06-11 13:08:04,738 DEBUG [org.springframework.orm.hibernate3.SessionFactoryU tils] Opening Hibernate Session
    2008-06-11 13:08:04,738 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 4969283931086848
    2008-06-11 13:08:04,738 DEBUG [org.springframework.orm.hibernate3.SessionFactoryU tils] Closing Hibernate Session
    2008-06-11 13:08:04,738 ERROR [com.x.y.z.ApplicationContext] Caught error in ApplicationContext init method
    2008-06-11 13:08:04,738 ERROR [com.x.y.z.ApplicationContext] org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    - mark

  • #2
    I thought I'd post my fix, which is more of a simple change of approach around what appears to be a Spring or Hibernate limitation - probably Hibernate, but I'm not sure. The fix is actually discussed in another thread on this forum, "Recommendation on transaction approach?", so see that thread. Essentially, it appears that Hibernate cannot manage more than one "current session", but it does happily manage that one current session, even though two are defined. But you can open the second session and close it using the fully functional secondly-defined session factory. I.e., you "manage" it yourself. Please note that this is not a good solution for multiple databases that truly require transaction management that spans all databases. In my case, I don't need that. So I simply define a session factory configured to the second datasource, and manipulate the session directly. The time I've wasted on this!

    Comment


    • #3
      This config, which is very similar to yours, works perfectly fine with spring 2.0.7 and 2.5.4, hibernate 3.2.4.sp1 and JBoss 4.2.2.GA (I know, these are newer versions than yours):
      Code:
      	<jee:jndi-lookup id="dataSource1" jndi-name="java:jdbc/oracle/test1"/>
      	<jee:jndi-lookup id="dataSource2" jndi-name="java:jdbc/oracle/test2"/>
      	
      	<bean id="sessionFactory1"
      		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      		<property name="dataSource" ref="dataSource1" />
      		<property name="annotatedClasses">
      		    <list>
      	    		<value>eu.bojar.jtatest.model.TestA</value>
      	     	</list>
         		</property>
      		<property name="hibernateProperties">
      			<value>
      				hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
      				hibernate.show_sql=true
      				hibernate.format_sql=true
      				hibernate.use_sql_comments=false
      				hibernate.hbm2ddl.auto=update
      			</value>
      		</property>
      	</bean>
      	
      	<bean id="sessionFactory2"
      		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
      		<property name="dataSource" ref="dataSource2" />
      		<property name="annotatedClasses">
      		    <list>
      	    		<value>eu.bojar.jtatest.model.TestB</value>
      	     	</list>
         		</property>
      		<property name="hibernateProperties">
      			<value>
      				hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
      				hibernate.show_sql=true
      				hibernate.format_sql=true
      				hibernate.use_sql_comments=false
      				hibernate.hbm2ddl.auto=update
      			</value>
      		</property>
      	</bean>
      
       	<bean id="transactionManager"
      		class="org.springframework.transaction.jta.JtaTransactionManager">
      	</bean>
      
      
      	<tx:annotation-driven transaction-manager="transactionManager"/>
      XA transactions and two phase commits are working. I used this config to test JTA and XA with Oracle and PostgreSQL. With PostgreSQL I had some issues when transactions failed during prepare phase (it can be corrected by changing some errorcodes in postgresql JDBC driver), but with Oracle it worked OK.

      Regards
      Jarek

      Comment


      • #4
        Hi Jarek,

        Thanks for your posting. I looked it over carefully, but I when I ran across this comment in the javadoc for AnnotationSessionFactoryBean, "Subclass of Spring's standard LocalSessionFactoryBean for Hibernate3, supporting JDK 1.5+ annotation metadata for mappings. Requires the Hibernate3 Annotation add-on to be present.", I decided to delay a test implementation, especially since I'm not using annotations. I don't have any more time for experimentation. I will try it later when I've got more time, though, and I appreciate the posting. Thank you!

        - Mark

        Comment

        Working...
        X