Results 1 to 5 of 5

Thread: Spring @Transactions and Hibernate Shards

  1. #1
    Join Date
    Jul 2007
    Posts
    101

    Default 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. #2
    Join Date
    Jan 2012
    Posts
    4

    Default

    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

  3. #3
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    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
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  4. #4
    Join Date
    Jan 2012
    Posts
    4

    Default

    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.

  5. #5
    Join Date
    Jan 2012
    Posts
    4

    Default

    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •