Results 1 to 10 of 10

Thread: springframework.orm.hibernate.HibernateTransaction Manager

  1. #1
    Join Date
    Aug 2004
    Posts
    15

    Default springframework.orm.hibernate.HibernateTransaction Manager

    Hi all,

    I am using Spring/Hibernate and i need some help !

    I'm using the org.springframework.orm.hibernate.HibernateTransac tionManager for test purposes and when i look at the logs , it works (!) but my Session is closed many times , so my entities are detached from their session and i lose the transparent persistence...

    My configuration must be wrong, thanks for any help


    Code:
    ...
    <bean id="managerTarget"
    		class="com.acme.ManagerImpl">
    		<property name="dao">
    			<ref bean="myDao"/>
    		</property>
    </bean>
    
    <bean id="manager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager">
    			<ref bean="transactionManager"/>
             </property>
    		<property name="target">
    			<ref bean="managerTarget"/>
             </property>
             <property name="transactionAttributes">
    			<props>
    			</props>
    		</property>
    	</bean>
    	
    	<!-- Transaction manager -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory"/>
    		</property>
        </bean>
    ...

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    First of all, you should declare what methods need to be proxied for transaction management in your services:
    Code:
      <property name="transactionAttributes"> 
        <props> 
           <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
           <prop key="*">PROPAGATION_REQUIRED</prop>
        </props> 
      </property>
    Now, for the session beeing closed and opened many times, could you provide an excerpt of your Test class and the equivalent log?
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Posts
    15

    Default

    i added the following lines but still have many Session...

    Code:
    <property name="transactionAttributes">
    	<props>
    		<prop key="*">PROPAGATION_REQUIRED</prop>
    	</props>
    </property>
    My test case log is the following...

    Code:
    INFO com.acme.Test - test&#40;&#41; START...
    
    DEBUG org.springframework.orm.hibernate.HibernateTransactionManager - Using transaction object &#91;org.springframework.orm.hibernate.HibernateTransactionObject@1bf7b23&#93;
    DEBUG org.springframework.orm.hibernate.HibernateTransactionManager - Creating new transaction
    DEBUG org.springframework.orm.hibernate.SessionFactoryUtils - Opening Hibernate session
    DEBUG net.sf.hibernate.impl.SessionImpl - opened session
    DEBUG org.springframework.orm.hibernate.HibernateTransactionManager - Opened new session &#91;net.sf.hibernate.impl.SessionImpl@ae4646&#93; for Hibernate transaction
    DEBUG org.springframework.orm.hibernate.HibernateTransactionManager - Beginning Hibernate transaction on session &#91;net.sf.hibernate.impl.SessionImpl@ae4646&#93;
    DEBUG net.sf.hibernate.transaction.JDBCTransaction - begin
    DEBUG net.sf.hibernate.transaction.JDBCTransaction - current autocommit status&#58;true
    DEBUG net.sf.hibernate.transaction.JDBCTransaction - disabling autocommit
    DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Bound value &#91;org.springframework.orm.hibernate.SessionHolder@1ce1bea&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@951a0&#93; to thread &#91;main&#93;
    DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Bound value &#91;org.springframework.jdbc.datasource.ConnectionHolder@14e0e90&#93; for key &#91;org.apache.commons.dbcp.BasicDataSource@10e434d&#93; to thread &#91;main&#93;
    DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Initializing transaction synchronization
    
    INFO com.acme.dao.DomainObjectDaoHibernateImpl - begin dao.create&#40;&#41;
    
    DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Retrieved value &#91;org.springframework.orm.hibernate.SessionHolder@1ce1bea&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@951a0&#93; bound to thread &#91;main&#93;
    DEBUG net.sf.hibernate.impl.SessionImpl - generated identifier&#58; 349440
    DEBUG net.sf.hibernate.impl.SessionImpl - saving &#91;com.acme.DomainObject#349440&#93;
    
    INFO com.acme.dao.DomainObjectDaoHibernateImpl - end dao.create&#40;&#41;
    
    DEBUG org.springframework.transaction.interceptor.TransactionInterceptor - Invoking commit for transaction on method 'createDomainObject' in class &#91;com.acme.DomainObjectManager&#93;
    INFO org.springframework.orm.hibernate.HibernateTransactionManager - Initiating transaction commit
    DEBUG org.springframework.orm.hibernate.HibernateTransactionManager - Committing Hibernate transaction on session &#91;net.sf.hibernate.impl.SessionImpl@ae4646&#93;
    DEBUG net.sf.hibernate.transaction.JDBCTransaction - commit
    DEBUG net.sf.hibernate.impl.SessionImpl - flushing session
    DEBUG net.sf.hibernate.impl.SessionImpl - Flushing entities and processing referenced collections
    DEBUG net.sf.hibernate.impl.SessionImpl - Processing unreferenced collections
    DEBUG net.sf.hibernate.impl.SessionImpl - Scheduling collection removes/&#40;re&#41;creates/updates
    DEBUG net.sf.hibernate.impl.SessionImpl - Flushed&#58; 1 insertions, 0 updates, 0 deletions to 1 objects
    DEBUG net.sf.hibernate.impl.SessionImpl - Flushed&#58; 0 &#40;re&#41;creations, 0 updates, 0 removals to 0 collections
    DEBUG net.sf.hibernate.impl.Printer - listing entities&#58;
    DEBUG net.sf.hibernate.impl.Printer - com.acme.DomainObject&#123;end_Getting_Data_Date=null, version_Date=14 September 2004 00&#58;00&#58;00, system_Date=14 September 2004 18&#58;39&#58;50, end_Date=null, end_Processing_Data_Date=null, state=0, lineCount=0, name=extractnametest, id=349440, version=null&#125;
    DEBUG net.sf.hibernate.impl.SessionImpl - executing flush
    DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity&#58; &#91;com.acme.DomainObject#349440&#93;
    DEBUG net.sf.hibernate.impl.SessionImpl - post flush
    DEBUG net.sf.hibernate.impl.SessionImpl - transaction completion
    DEBUG net.sf.hibernate.transaction.JDBCTransaction - re-enabling autocommit
    DEBUG org.springframework.orm.hibernate.HibernateTransactionManager - Triggering afterCompletion synchronization
    DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Clearing transaction synchronization
    DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Removed value &#91;org.springframework.jdbc.datasource.ConnectionHolder@14e0e90&#93; for key &#91;org.apache.commons.dbcp.BasicDataSource@10e434d&#93; from thread &#91;main&#93;
    DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Removed value &#91;org.springframework.orm.hibernate.SessionHolder@1ce1bea&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@951a0&#93; from thread &#91;main&#93;
    
    DEBUG org.springframework.orm.hibernate.HibernateTransactionManager - Closing Hibernate session &#91;net.sf.hibernate.impl.SessionImpl@ae4646&#93; after transaction
    DEBUG org.springframework.orm.hibernate.SessionFactoryUtils - Closing Hibernate session
    DEBUG net.sf.hibernate.impl.SessionImpl - closing session
    
    
    DEBUG net.sf.hibernate.impl.SessionImpl - disconnecting session
    DEBUG net.sf.hibernate.impl.SessionImpl - transaction completion
    
    INFO com.acme.dao.DomainObjectDaoHibernateImpl - begin dao.loadByName&#40;name&#41;
    
    
    DEBUG org.springframework.orm.hibernate.SessionFactoryUtils - Opening Hibernate session
    DEBUG net.sf.hibernate.impl.SessionImpl - opened session
    ..
    
    INFO com.acme.Test - test&#40;&#41; END...
    [/code]

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    fabrice,

    If I understand the log, a new Hibernate Session / Transaction is bound for each dao method call. I can not say if this is not anormal since I do not know how your Service / DAO are bounds. Do you have a case when a service method calls two or more DAO methods?

    In general, wrapping service methods using transactions will cause Spring to create a new Hibernate session / transaction whenever you call a service method and remove the session after the method returns. If you need to wrap multiple methods inside the same transaction, you must create a new method that calls your initial methods and apply transaction demarcation to the new method.

    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5
    Join Date
    Aug 2004
    Posts
    15

    Default

    i understand ....

    in fact, the BIG problem is that my service can't be "Springed" because it's an EJB environment (we are in migration..) so my Spring manager is behind the layer service...no way to avoid it

    In the app server environment, i think that using the TransacationMananger of Weblogic, the transaction will be propagated and it should work....

    In the test environment, i use org.springframework.orm.hibernate.HibernateTransac tionManager

    Is it possible to open the Session at the beginning of the test() method and close it at the end of the test() method and HibernateTransactionManager would use it ?

    I know it's a little be complicated, it's just for using test and simulating the propagation of my transaction without changing my production code...

  6. #6
    Join Date
    Aug 2004
    Posts
    15

    Default Used instead a JTA

    I changed my TransactionMangager to use JTA/JOTM which is supported by Spring


    Code:
    <property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</prop>
                    <prop key="hibernate.transaction.manager_lookup_class">net.sf.hibernate.transaction.JOTMTransactionManagerLookup</prop>
                    <prop key="jta.UserTransaction">java&#58;comp/UserTransaction</prop>
    			</props>
    		</property>


    Code:
    INFO com.acme.service.NewFileEventHandlerTestMocked - test&#40;&#41; START...
    
    INFO com.acme.service.NewFileEventHandlerTestMocked - userTransaction.begin&#40;&#41;
    Transaction JTA began...

    Code:
    DEBUG org.springframework.transaction.interceptor.TransactionInterceptor - Getting transaction for method 'createAcmeObject' in class &#91;com.acme.manager.AcmeObjectManager&#93;
    DEBUG org.springframework.transaction.jta.JtaTransactionManager - Using transaction object &#91;org.objectweb.jotm.Current@19e733e&#93;
    DEBUG org.springframework.transaction.jta.JtaTransactionManager - Participating in existing transaction
    DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Initializing transaction synchronization
    My JOTM transaction is used...cool!

    Code:
    INFO com.acme.dao.AcmeObjectDao - begin dao.create&#40;name,date&#41;
    
    DEBUG org.springframework.orm.hibernate.SessionFactoryUtils - Opening Hibernate session
    DEBUG net.sf.hibernate.impl.SessionImpl - opened session
    DEBUG org.springframework.orm.hibernate.SessionFactoryUtils - Registering Spring transaction synchronization for Hibernate session
    DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Bound value &#91;org.springframework.orm.hibernate.SessionHolder@1b5eba4&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@1b00766&#93; to thread &#91;main&#93;
    DEBUG net.sf.hibernate.impl.SessionImpl - generated identifier&#58; 349501
    DEBUG net.sf.hibernate.impl.SessionImpl - saving &#91;com.acme.domain.AcmeObject#349501&#93;
    
    INFO com.acme.dao.AcmeObjectDao - end dao.create&#40;&#41;
    why does Spring still invoke a commit at the end of my dao method ???

    Code:
    DEBUG org.springframework.transaction.interceptor.TransactionInterceptor - Invoking commit for transaction on method 'createAcmeObject' in class &#91;com.acme.manager.AcmeObjectManager&#93;
    DEBUG org.objectweb.jotm.jta - status=STATUS_ACTIVE
    DEBUG org.springframework.transaction.jta.JtaTransactionManager - Triggering beforeCommit synchronization
    DEBUG net.sf.hibernate.impl.SessionImpl - Flushed&#58; 1 insertions, 0 updates, 0 deletions to 1 objects
    DEBUG net.sf.hibernate.impl.SessionImpl - executing flush
    
    DEBUG net.sf.hibernate.SQL - insert into ACME_TABLE ...
    DEBUG org.springframework.transaction.jta.JtaTransactionManager - Triggering beforeCompletion synchronization
    DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Removed value &#91;org.springframework.orm.hibernate.SessionHolder@1b5eba4&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@1b00766&#93; from thread &#91;main&#93;
    DEBUG org.springframework.orm.hibernate.SessionFactoryUtils - Closing Hibernate session
    2004-09-15 12&#58;19&#58;08,156 DEBUG org.springframework.orm.hibernate.SessionFactoryUtils - Closing Hibernate session
    2004-09-15 12&#58;19&#58;08,156 DEBUG net.sf.hibernate.impl.SessionImpl - closing session
    2004-09-15 12&#58;19&#58;08,156 DEBUG net.sf.hibernate.impl.SessionImpl - disconnecting session
    2004-09-15 12&#58;19&#58;08,156 DEBUG org.springframework.transaction.jta.JtaTransactionManager - Triggering afterCompletion synchronization
    2004-09-15 12&#58;19&#58;08,156 DEBUG org.springframework.transaction.support.TransactionSynchronizationManager - Clearing transaction synchronization
    INFO org.objectweb.jotm - set rollback only
    DEBUG org.objectweb.jotm.jta - status=STATUS_MARKED_ROLLBACK
    DEBUG org.objectweb.jotm.jta - Commit local transaction -> rolled back!
    And my JTA transaction is rolled back ??


    My test case is bellow

    Code:
    transactionJTA.begin&#40;&#41;;
    AcmeObject newAcme=service.createAcmeObject&#40;...&#41;; //2 DAO calls in this method
    newAcme.setName&#40;"name"&#41;;
    transactionJTA.commit&#40;&#41;;
    I'm sure i made a mistake but if anyone can help me....

  7. #7
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Is it possible to open the Session at the beginning of the test() method and close it at the end of the test() method and HibernateTransactionManager would use it ?
    take a look at this thread Unit tests and LazyInitializationException
    Last edited by Rod Johnson; Jan 18th, 2006 at 10:57 AM.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  8. #8

    Default

    How can I do for get the DEBUG information?No matter what I seem to do with TransactionProxyFactoryBean I can't get it to recognize this method call as a target, or produce any sort of debugging output from the DebugInterceptor as a preInterceptor that I tried to configure.

  9. #9
    Join Date
    Aug 2004
    Posts
    15

    Default Debugging log

    To get the logs, i put the following log4j.properties in my classpath

    Code:
    # Log all messages down to level INFO.
    # Write messages to console and to example.log file.
    log4j.rootLogger=DEBUG, stdout
    
    #filters on hibernate logs
    log4j.logger.net.sf.hibernate.hql.QueryTranslator=debug
    log4j.logger.net.sf.hibernate.SQL=debug
    log4j.logger.net.sf.hibernate.type=info
    log4j.logger.net.sf.hibernate.impl.BatcherImpl=info
    
    # Defines the console logger.
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d %p %c - %m%n

  10. #10
    Join Date
    Aug 2004
    Posts
    15

    Default Thank you

    Thanks Omar,

    i didn't see this thread and now my test is woking !

Posting Permissions

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