Announcement Announcement Module
Collapse
No announcement yet.
Hiberante transaction doesn't work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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, 10:19 AM.

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


        • #5
          Can you post your stacktrace and the test code you are using?
          Last edited by karldmoore; Aug 29th, 2007, 10:18 AM.

          Comment


          • #6
            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.

            Comment


            • #7
              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

              Comment

              Working...
              X