Results 1 to 7 of 7

Thread: Hiberante transaction doesn't work

  1. #1

    Default Hiberante transaction doesn't work

    I am using Tomcat 5.5, Oracle Express and Spring 2.6. Now I have problems in transaction. I did all setup, but transaction doesn't work.

    Code:
    <bean id="MtsDao" class="dao.MtsDaoImpl">
    		<property name="sessionFactory" ref="sessionFactory" />
    	</bean>	
    	
    	<bean id="mtsTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory" /> 
    	</bean>
    
    	<bean id="MtsServiceTarget" class="service.MtsServiceImpl">
    		<property name="mtsDao" ref="MtsDao" />
    	</bean>
    	
    	<bean id="MtsService"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager" ref="mtsTxManager"/>
        <property name="target" ref="MtsServiceTarget"/>
        <property name="transactionAttributes">
          <props>
            <prop key="increasePrice">PROPAGATION_REQUIRED</prop>        
            <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
          </props>
        </property>
      </bean>
      
      	<!--  ///////////////// Hibernate ///////////  -->
    	<!-- DataSource Property -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName">
    			<value>oracle.jdbc.driver.OracleDriver</value>
    		</property>
    		<property name="url">
    			<value>jdbc:oracle:thin:@localhost:1521/XE</value>
    		</property>
    		<property name="username" value="t" />
    		<property name="password" value="t" />
    	</bean>
    
    	<!-- Database Property -->
    	<bean id="hibernateProperties"
    		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    		<property name="properties">
    			<props>
    				<prop key="hibernate.hbm2ddl.auto">create</prop>
    				<prop key="hibernate.dialect">
    					org.hibernate.dialect.OracleDialect
    				</prop>				
    				
    				<prop key="hibernate.cache.use_query_cache">true</prop>
    				<prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>				
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.c3p0.minPoolSize">5</prop>
    				<prop key="hibernate.c3p0.maxPoolSize">20</prop>
    				<prop key="hibernate.c3p0.timeout">600</prop>
    				<prop key="hibernate.c3p0.max_statement">50</prop>				
    				<prop key="hibernate.c3p0.testConnectionOnCheckout">
    					false
    				</prop>
    			</props>
    		</property>
    	</bean>
    
    	<!-- Hibernate SessionFactory -->
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource">
    			<ref local="dataSource" />
    		</property>
    		<property name="hibernateProperties">
    			<ref bean="hibernateProperties" />
    		</property>		
    		<property name="mappingResources">
    			<list>
    				<value>project.hbm.xml</value>
    			</list>
    		</property>
    	</bean>

    In the Dao,
    Code:
    public void addPeople(String name){
    		People p = new People();
    		p.setTitle(name);
    		Ticket t = new Ticket();
    		List ticketList = new ArrayList();
    		ticketList.add(t);
    		p.setTicketList(ticketList);		
    		this.getHibernateTemplate().saveOrUpdate(p);		
    	}
    In the service,
    Code:
    public void increasePrice() throws Exception {
    		//System.out.println("---invoke dao, get result="+mtsDao.getPeople());
    		mtsDao.addPeople("manager4");
    		System.out.println("---throw excetpion, test transaction---");
    		if(true)
    			throw new Exception("test transaction");
    		mtsDao.addPeople("staff4");
    		
    	}
    test transaction, but failed, the data is not roll-back.

    Any hints ? Did I miss anything ?

    Thanks.

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    You've thrown an Exception which isn't rolled back by default, if you make this RuntimeException it would work. Otherwise you need to tell the TransactionProxyFactoryBean to rollback for Exception.
    In this example, note that the value for the insert* mapping contains a rollback rule. Adding -MyCheckedException here specifies that if the method throws MyCheckedException or any subclasses, the transaction will automatically be rolled back. Multiple rollback rules can be specified here, comma-separated. A - prefix forces rollback; a + prefix specifies commit. (This allows commit even on unchecked exceptions, if you really know what you're doing!)
    http://static.springframework.org/sp...n.html#d0e5690
    Last edited by karldmoore; Aug 29th, 2007 at 10:19 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3
    Join Date
    Nov 2006
    Location
    Germany
    Posts
    452

    Default

    Hello,

    Spring only rollbacks an exception if there is any runtime exception thrown (or an error). In your case this is not the reason, that's the reasin why the Transaction is commited.

    Have a look in the reference Manual here

    regards
    agim

  4. #4

    Default

    Quote Originally Posted by Lyserg View Post
    Spring only rollbacks an exception if there is any runtime exception thrown (or an error). In your case this is not the reason, that's the reasin why the Transaction is commited.
    Thanks. Even I force it throw RuntimeException, still NOT work. Maybe something else.

    Do I need to set up hibernate transaction factory ? or some other things ?

    Thanks.

  5. #5
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Can you post your stacktrace and the test code you are using?
    Last edited by karldmoore; Aug 29th, 2007 at 10:18 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  6. #6

    Default

    Quote Originally Posted by karldmoore View Post
    Can you post your stacktrace and the test code you are using?
    Kardmoore,

    Thanks for your quick reply. Here is my log attachment.

    Thanks.
    Attached Files Attached Files

  7. #7

    Default

    I think I found the reason.

    The reason is I wire wrong service to controller. I should wire "MtsService", instead of "MtsServiceTarget".

    Much appreciated your guys reply. This forum always gives me lot of help.

    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
  •