Announcement Announcement Module
Collapse
No announcement yet.
Hib3 Spring 2.5 TX Problem (Works Fine With Spring 2.0.7) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hib3 Spring 2.5 TX Problem (Works Fine With Spring 2.0.7)

    Hello,

    I am trying to implement a direct hibernate 3 dao class (See below). When trying to invoke the addCompany method I get the error message "HibernateSystemException: saveOrUpdate is not valid without active transaction; nested exception is org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction"

    I know I am using the correct proxied version as I can see in the debug logs it attempt to setup the transaction (see below).

    If I swap out the Spring 2.5 libraries for the Spring 2.0.7 libraries everything works fine. Is this a known issue with Spring 2.5?

    Class
    Code:
    Repository
    public class CompanyHibernateDao implements CompanyDao {
    	
    	private SessionFactory sessionFactory;
    
    	@Transactional
    	public void addComany(Company company) {
    		Session session = sessionFactory.getCurrentSession();
    		session.saveOrUpdate(company);
    	}
    	
    	/**
    	 * @return
    	 */
    	public SessionFactory getSessionFactory() {
    		return sessionFactory;
    	}
    	public void setSessionFactory(SessionFactory sessionFactory) {
    		this.sessionFactory = sessionFactory;
    	}
    }
    Debug Logs
    Code:
    18 Dec 2007 15:12:48 [Level:DEBUG]Adding transactional method [addComany] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT]
    18 Dec 2007 15:12:48 [Level:DEBUG]Using transaction object [org.springframework.orm.hibernate3.HibernateTransactionManager$HibernateTransactionObject@31123112]
    18 Dec 2007 15:12:48 [Level:DEBUG]Creating new transaction with name [cory.stock.core.dao.CompanyDao.addComany]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
    18 Dec 2007 15:12:48 [Level:DEBUG]opened session at timestamp: 11980223888
    18 Dec 2007 15:12:48 [Level:DEBUG]Opened new Session [org.hibernate.impl.SessionImpl@3bba3bba] for Hibernate transaction
    18 Dec 2007 15:12:48 [Level:DEBUG]Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@3bba3bba]
    18 Dec 2007 15:12:48 [Level:DEBUG]begin
    18 Dec 2007 15:12:48 [Level:DEBUG]opening JDBC connection
    18 Dec 2007 15:12:48 [Level:DEBUG]current autocommit status: true
    18 Dec 2007 15:12:48 [Level:DEBUG]disabling autocommit
    18 Dec 2007 15:12:48 [Level:DEBUG]Exposing Hibernate transaction as JDBC transaction [jdbc:derby://localhost:1527/stockdb, UserName=APP, Apache Derby Network Client JDBC Driver]
    18 Dec 2007 15:12:48 [Level:DEBUG]Bound value [org.springframework.jdbc.datasource.ConnectionHolder@3dce3dce] for key [org.apache.commons.dbcp.BasicDataSource@70ea70ea] to thread [main]
    18 Dec 2007 15:12:48 [Level:DEBUG]Bound value [org.springframework.orm.hibernate3.SessionHolder@8900890] for key [org.hibernate.impl.SessionFactoryImpl@3ce03ce] to thread [main]
    18 Dec 2007 15:12:48 [Level:DEBUG]Initializing transaction synchronization
    18 Dec 2007 15:12:48 [Level:DEBUG]Enter: addComany
    18 Dec 2007 15:12:48 [Level:DEBUG]Arg 1=cory.stock.core.model.impl.DefaultCompany@35ba35ba
    	[businessNumber=hibDefaultCompany
    	,stock=null
    	,outstandingShares=1
    	,cashReserves=1
    	,marketCaptilization=1]
    18 Dec 2007 15:12:48 [Level:DEBUG]opened session at timestamp: 11980223889
    18 Dec 2007 15:12:48 [Level:DEBUG]Thrown Exception: type=org.springframework.orm.hibernate3.HibernateSystemException
    18 Dec 2007 15:12:48 [Level:DEBUG]Thrown Exception: message=saveOrUpdate is not valid without active transaction; nested exception is org.hibernate.HibernateException: saveOrUpdate is not valid without active transaction
    18 Dec 2007 15:12:48 [Level:DEBUG]Exit: addComany
    18 Dec 2007 15:12:48 [Level:DEBUG]Triggering beforeCompletion synchronization
    18 Dec 2007 15:12:48 [Level:DEBUG]Initiating transaction rollback
    18 Dec 2007 15:12:48 [Level:DEBUG]Rolling back Hibernate transaction on Session [org.hibernate.impl.SessionImpl@3bba3bba]
    18 Dec 2007 15:12:48 [Level:DEBUG]rollback
    18 Dec 2007 15:12:48 [Level:DEBUG]re-enabling autocommit
    18 Dec 2007 15:12:48 [Level:DEBUG]rolled back JDBC Connection
    18 Dec 2007 15:12:48 [Level:DEBUG]transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
    18 Dec 2007 15:12:48 [Level:DEBUG]Triggering afterCompletion synchronization
    18 Dec 2007 15:12:49 [Level:DEBUG]Clearing transaction synchronization
    18 Dec 2007 15:12:49 [Level:DEBUG]Removed value [org.springframework.orm.hibernate3.SessionHolder@8900890] for key [org.hibernate.impl.SessionFactoryImpl@3ce03ce] from thread [main]
    18 Dec 2007 15:12:49 [Level:DEBUG]Removed value [org.springframework.jdbc.datasource.ConnectionHolder@3dce3dce] for key [org.apache.commons.dbcp.BasicDataSource@70ea70ea] from thread [main]
    18 Dec 2007 15:12:49 [Level:DEBUG]Closing Hibernate Session [org.hibernate.impl.SessionImpl@3bba3bba] after transaction
    18 Dec 2007 15:12:49 [Level:DEBUG]Closing Hibernate Session
    18 Dec 2007 15:12:49 [Level:DEBUG]releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
    18 Dec 2007 15:12:49 [Level:DEBUG]transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
    Hibernate Config
    Code:
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    
    	<!-- Transaction manager for hibernate -->
    	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    
    	<!-- DAOs-->
    	<bean id="companyDao" class="cory.stock.core.dao.impl.CompanyHibernateDao">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    	
    	<!-- Hibernate Session Factory -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"/>
      		<property name="mappingLocations">
      			<list>
      				<value>classpath:Company.hbm.xml</value>
            		<value>classpath:Stock.hbm.xml</value>
            		<value>classpath:StockExchange.hbm.xml</value>
      			</list>
      		</property>
      		<property name="hibernateProperties"><ref local="hibernateProperties"/></property>
    	</bean>
    	
    	<!-- This configures default properties, which can overridden with the file specified by the location property -->
        <bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
             <property name="properties">
                <props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
                    <prop key="hibernate.current_session_context_class">thread</prop>
    				<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop> 
                    <prop key="hibernate.connection.release_mode">on_close</prop>
                    <!-- <prop key="hibernate.connection.release_mode">on_close</prop> -->
                </props>
            </property>
        </bean>
    	
    	<!-- Data Source -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      		<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
    		<property name="url" value="jdbc:derby://localhost:1527/stockdb"/>
    		<property name="initialSize" value="1"/>
    	</bean>

  • #2
    Remove the transactional stuff from your hibernate properties.

    Code:
    <bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
      <property name="properties">
        <props>
          <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop>
          <prop key="hibernate.show_sql">true</prop>
          <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
          <prop key="hibernate.current_session_context_class">thread</prop>
          <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop> 
          <prop key="hibernate.connection.release_mode">on_close</prop>
          <!-- <prop key="hibernate.connection.release_mode">on_close</prop> -->
        </props>
      </property>
    </bean>
    You want spring to manage transactions, not hibernate.

    Comment


    • #3
      That has solved the issue, appreciate the assistance.

      Comment


      • #4
        Your welcome, glad it worked.

        Comment

        Working...
        X