Results 1 to 3 of 3

Thread: TX not rolling back when Exception thrown

  1. #1
    Join Date
    Dec 2005
    Posts
    6

    Default TX not rolling back when Exception thrown

    When and exception is thrown the transaction should roll back if i'm reading things right. I saw a similar thread where REQUIRES_NEW was not working, I wonder if this is related.

    Here is the relevant pieces from my applicationContext.xml, I did leave out the SessionFactory.

    Code:
    	<bean id="tkdSiteDS"
    		class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName"
    			value="com.mysql.jdbc.Driver">
    		</property>
    		<property name="url"
    			value="jdbc:mysql://localhost/dbname?autoReconnection=true">
    		</property>
    		<property name="username" value="username"></property>
    		<property name="password" value="password"></property>
    	</bean>
    
    	<bean id="transactionManager"
    		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory">
    			<ref bean="SessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="StudentManager"
    		class="com.caughey.tkd.biz.student.impl.StudentManagerImpl"
    		scope="singleton">
    		<property name="studentDAO">
    			<ref bean="StudentDAO" />
    		</property>
    		<property name="addressDAO">
    			<ref bean="AddressDAO" />
    		</property>
    		<property name="studentAddressDAO">
    			<ref bean="StudentAddressDAO" />
    		</property>
    		<property name="studentContactDAO">
    			<ref bean="StudentContactDAO" />
    		</property>
    		<property name="addressTypeCache">
    			<ref bean="AddressTypeCache" />
    		</property>
    		<property name="sessionFactory">
    			<ref bean="SessionFactory" />
    		</property>
    	</bean>
    
    	<bean id="StudentManagerTX"
    		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager" ref="transactionManager" />
    		<property name="transactionAttributes">
    			<props>
    				<prop key="addStudent">
    					PROPAGATION_REQUIRED,-com.caughey.tkd.biz.student.exceptions.UnableToAddStudentException
    				</prop>
    				<prop key="retrieveStudents">
    					PROPAGATION_NOT_SUPPORTED
    				</prop>
    			</props>
    		</property>
    		<property name="target">
    			<ref local="StudentManager" />
    		</property>
    	</bean>

    Here is the code that is throwing the Exception, I placed the exception inline to test the rollback function.

    Code:
    	public Integer addStudent(StudentFull student)
    			throws UnableToAddStudentException {
    
    		Integer studentId = null;
    		Address address = student.getAddress();
    		Collection<StudentContact> contacts = student.getContacts();
    
    		try {
    			// Save the contacts
    			if (contacts != null) {
    				Iterator<StudentContact> itt = contacts.iterator();
    				StudentContact studentContact = null;
    				while (itt.hasNext()) {
    					studentContact = itt.next();
    					if (studentContact.getContactTypeId().intValue() == 7) {
    						logger.info("Throwing Exception");
    						throw new Exception();
    					}
    					studentContact.setStudentId(studentId);
    					studentContactDAO.save(studentContact);
    				}
    			}
    		} catch (Exception e) {
    			studentId = null;
    			throw new UnableToAddStudentException("Problem saving contacts");
    		}
    
    		return studentId;
    	}

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Make sure that you use the proxied instance of your UserManager and not the unproxied instance. If you use Spring 2.0 or higher I suggest you use the aop:config stuff, that makes it more difficult to obtain the unproxied instance.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Dec 2005
    Posts
    6

    Default

    Ah so in the example above I would use StudentManagerTX not StudentManager. Correct?

Posting Permissions

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