Announcement Announcement Module
Collapse
No announcement yet.
TX not rolling back when Exception thrown Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


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

      Comment

      Working...
      X