Announcement Announcement Module
Collapse
No announcement yet.
Spring @Transactions and Hibernate Shards Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring @Transactions and Hibernate Shards

    I am trying to provide a Spring compatible layer that works with Hibernate Shards and I have it mostly working. I keep running into the error where the TransactionSynchronizationManager.unbind is being called twice, but bind is being called once. Here is the partial stack trace:

    java.lang.IllegalStateException: No value for key [org.hibernate.shards.session.ShardedSessionFactory Impl@644ca6b6] bound to thread [main]
    at org.springframework.transaction.support.Transactio nSynchronizationManager.unbindResource(Transaction SynchronizationManager.java:184)
    at org.springframework.orm.hibernate3.HibernateTransa ctionManager.doCleanupAfterCompletion(HibernateTra nsactionManager.java:647)
    at org.springframework.transaction.support.AbstractPl atformTransactionManager.cleanupAfterCompletion(Ab stractPlatformTransactionManager.java:966)
    at org.springframework.transaction.support.AbstractPl atformTransactionManager.processCommit(AbstractPla tformTransactionManager.java:759)
    at org.springframework.transaction.support.AbstractPl atformTransactionManager.commit(AbstractPlatformTr ansactionManager.java:678)
    at org.springframework.transaction.interceptor.Transa ctionAspectSupport.commitTransactionAfterReturning (TransactionAspectSupport.java:319)
    at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:116)
    at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :171)
    at org.springframework.aop.framework.Cglib2AopProxy$D ynamicAdvisedInterceptor.intercept(Cglib2AopProxy. java:635)
    at com.reardencommerce.kernel.shards.res.repository.S hardTestRepository$$EnhancerByCGLIB$$2a0f4f34.save ShardEntity(<generated>)

    The first unbind removes the map. Does anyone know why it is being called twice? I have the method boolean isSameConnectionForEntireSession(Session session) return false since shards does not support the getCurrentSession(). It throws UnsupportedOperationException()

  • #2
    hi,
    i am trying to integrate spring with hibernate shards. Sharding seems to be working on app side (looking at saved object IDs) but i do not see the data in DB. It increments PK correctly but entries never appear in DB. Is LocalSessionFactoryBean reqd? Thnx

    Comment


    • #3
      Hello

      Next time use the code tags

      About the error

      Code:
      java.lang.IllegalStateException: 
      No value for key [org.hibernate.shards.session.ShardedSessionFactory Impl@644ca6b6] bound to thread [main]
      at org.springframework.transaction.support.Transactio nSynchronizationManager.unbindResource(Transaction SynchronizationManager.java:184)
      Post your actual configuration to get a better idea

      Comment


      • #4
        Thank you for the response.

        I figured the issue is with transactions.

        in my Dao.save() i added transaction code and now i see the entries in DB.
        This does not seem right, we cannot commit on each save...

        Code:
         public PK save (E e) {
            	
            	Session s = this.getSessionFactory().openSession(); 
            	s2.flush();
        	Transaction tx =s2.beginTransaction();
        		
        	Object o = s2.save(e);
        	tx.commit();
        			
        	s2.flush();
                s2.close();
        	return (PK) o;
        Also getCurrentSession() on SharededSessionFactory throws "UnsupportedOperationException"

        Without Sharding, my configuration in XML was:

        Code:
        <bean id="sessionFactory"
        		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        		<property name="dataSource" ref="dataSource" />
        		<property name="mappingResources">
                          ....
        </bean>
        Now, for sharding, i am using my own ShardedSessionFactoryBuilder class as described in:
        http://www.ibm.com/developerworks/ja...j-javadev2-11/

        So there is no "LocalSessionFactoryBean". Is this needed for Transactions?

        thanks.

        Comment


        • #5
          Also, another option is to use AbstractRoutingDataSource.
          http://blog.springsource.com/main/20...ource-routing/

          My understanding is if i have same schema on all nodes, i can use this approach with a single Session factory and supply the right datasource (for every thread context). All data for any request will always be saved in a single node (no cross database queries ever). In terms of second level cache issues with this approach, using UUID as PK can resolve that issue..

          Any other major reasons for not using AbstractRoutingDataSource?

          Thanks!

          Comment

          Working...
          X