Announcement Announcement Module
Collapse
No announcement yet.
Recommendation on transaction approach? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Recommendation on transaction approach?

    I've got another open thread (Having real trouble configuring JTA txManagement w/ JBoss & Hibernate), but I thought I'd take a step back and ask a more fundamental question.

    I've got an application that needs access to two separate databases. The app will only access these databases read-only, so no commit, rollback, etc. needed. I tried creating two separate Hibernate session factories, with a single Hibernate transaction manager; didn't work. I then tried two separate Hibernate session factories, with two Hibernate transaction managers, one per; didn't work. I then saw a mention somewhere that if you have two datasources, you need to use JTA. So I tried it, and can not get it configured properly.

    It's hard for me to grasp why I can't just use Hibernate (within Spring) to manage two session factories, each one with a separate db connection. Do I really need to use JTA? The usage of these databases are completely separate one from the other.

    If the answer is yes, as I suspect, then does anyone know how to configure this scenario -- see above link.

    Thanks!

  • #2
    No, you absolutely need not to use JTA. Even with read/write access, as far as your transactions does not span database boundaries.

    What means "does not work"? Can you elaborate on it?

    Regards,
    Oleksandr
    Originally posted by mal316 View Post
    I've got another open thread (Having real trouble configuring JTA txManagement w/ JBoss & Hibernate), but I thought I'd take a step back and ask a more fundamental question.

    I've got an application that needs access to two separate databases. The app will only access these databases read-only, so no commit, rollback, etc. needed. I tried creating two separate Hibernate session factories, with a single Hibernate transaction manager; didn't work. I then tried two separate Hibernate session factories, with two Hibernate transaction managers, one per; didn't work. I then saw a mention somewhere that if you have two datasources, you need to use JTA. So I tried it, and can not get it configured properly.

    It's hard for me to grasp why I can't just use Hibernate (within Spring) to manage two session factories, each one with a separate db connection. Do I really need to use JTA? The usage of these databases are completely separate one from the other.

    If the answer is yes, as I suspect, then does anyone know how to configure this scenario -- see above link.

    Thanks!

    Comment


    • #3
      Hi Oleksandr,

      Thanks for your reply. In answer to your question, I had set up my Spring configuration to have 2 session factories, each with it's own datasource, each with it's own transaction manager, and each with it's own transaction 'advice'. When I run the app., the first session factory produces a current session, and I can perform queries successfully with it. The second session factory throws the following error when I attempt to get a current session from it:

      org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

      Here are a few logs from the point where I'm trying to get the current session from the second session factory:

      2008-06-12 12:00:56,525 DEBUG [org.springframework.orm.hibernate3.SessionFactoryU tils] Opening Hibernate Session
      2008-06-12 12:00:56,525 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 4969621325926400
      2008-06-12 12:00:56,525 DEBUG [org.springframework.orm.hibernate3.SessionFactoryU tils] Closing Hibernate Session
      2008-06-12 12:00:56,525 ERROR [gov.nih.nci.cma.util.ApplicationContext] Caught error in ApplicationContext init method
      2008-06-12 12:00:56,525 ERROR [gov.nih.nci.cma.util.ApplicationContext] org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

      - here is the configuration that I have:

      HTML Code:
      	<bean id="sessionFactory1"
      		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      		<property name="configLocation">
      			<value>/WEB-INF/domain1-hibernate.cfg.xml</value>
      		</property>
      	</bean>
      	
      	<bean id="sessionFactory2"
      		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      		<property name="configLocation">
      			<value>/WEB-INF/domain2-hibernate.cfg.xml</value>
      		</property>
      	</bean>
      
      	<bean id="txManager1"
      		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      		<property name="sessionFactory" ref="sessionFactory1" />
      	</bean>
      
      	<bean id="txManager2"
      		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      		<property name="sessionFactory" ref="sessionFactory2" />
      	</bean>
      	
      
      	<tx:advice id="txAdvice1" transaction-manager="txManager1">
      		<tx:attributes>
      			<tx:method name="*" read-only="true"/>
      		</tx:attributes>
      	</tx:advice>
      
      	<tx:advice id="txAdvice2" transaction-manager="txManager2">
      		<tx:attributes>
      			<tx:method name="*" read-only="true"/>
      		</tx:attributes>
      	</tx:advice>
      What do you think might be the problem? In the two Hibernate config files that I have, each has the following property defined:
      <property name="current_session_context_class">thread</property>

      Thanks for any help that you can give!

      Comment


      • #4
        Okay, I've got a little additional information to relay. Rather than getting the "current session" from the sessionFactory, I opened a session ( sessionFactory.openSession() ). This worked, and I was able to successfully query. So the problem appears to be that Hibernate is only able to bind and manage one "current session", at least if you're using transaction management. Seems like an unfortunate limitation.

        Comment


        • #5
          openSession() does not go along with the Spring session/transaction management. Spring just does not know about this sessions. In your specifc "read-only" situation it may work (as you really need not transactions).

          Concerning your configuration - you should not specify
          Code:
          <property name="current_session_context_class">thread</property>
          in your Hibernate config files, Spring sets context class for Hibernate itself, and it is Spring's own implementation. So your setting may do harm.

          And concerning your transaction advices - from your configuration snippet is absolutely unclear which advice is applied to which method as you have not included advisor configuration. And class/method names would be useful as well.

          Originally posted by mal316 View Post
          Okay, I've got a little additional information to relay. Rather than getting the "current session" from the sessionFactory, I opened a session ( sessionFactory.openSession() ). This worked, and I was able to successfully query. So the problem appears to be that Hibernate is only able to bind and manage one "current session", at least if you're using transaction management. Seems like an unfortunate limitation.

          Comment


          • #6
            Hi Oleksandr,

            Thanks again for your reply! You're right that I did not include the advice to methods configuration -- I did not think they were necessary in the context of my problem, so I did not include them, but they are there in the config file.

            I tried removing the '<property name="current_session_context_class">thread</property>' from the Hibernate config file, and tried retrieving the current session again, but I got the same result as always. But that's a good thing to know, nonetheless.

            Yes, I understand that my solution operates outside of any transaction management, or rather, if I need it, I'll have to provide it myself. That's why I said that the current behavior is unfortunate. I'd rather have a cleaner solution with Spring-Hibernate managed transactions (even though I don't have a business model that requires that), but I need to move on. I've spent more than two days on this. And you, Oleksandr, are the only one who replied so I truly thank you.

            Comment

            Working...
            X