Announcement Announcement Module
Collapse
No announcement yet.
Need help on JDBC Rollback.. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need help on JDBC Rollback..

    Hi folks,

    Here's the scope: I have a class named springProfileDAO which needs to update two other DAOs attached to it. When updating, if an update fails, I want to rollback on everything (rollback on profile plus allocationCustomerDetailDAO and springAllocationProfileExceptionDAO).

    Here's the config.

    Code:
    <beans>
    
    	<bean id="profileService" class="ca.cn.imx.services.AllocationProfileService" />
    
    
    	<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName">
    			<value>jdbc/db2legacy</value>
    		</property>
    	</bean>
    
    <bean id="transactionManager" 
    	    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	    <property name="dataSource"><ref local="dataSource"/></property>
    </bean>
    
    	<bean id="proxy"
    	    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    	    <property name="transactionManager"><ref bean="transactionManager"/></property>
    	    <property name="target"><ref local="springProfileDAO"/></property>
       	    <property name="target"><ref local="allocationCustomerDetailDAO"/></property>
       	    <property name="target"><ref local="springAllocationProfileExceptionDAO"/></property>   	    
    	    <property name="transactionAttributes">
    	        <props>
    	            <prop key="add*">PROPAGATION_REQUIRED,-DataAccessException</prop>
    	            <prop key="update*">PROPAGATION_REQUIRED,-DataAccessException</prop>
    	            <prop key="delete*">PROPAGATION_REQUIRED,-DataAccessException</prop>	            
    	            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    	        </props>
    	    </property>
    	</bean>
    	<bean id="springProfileDAO" class="ca.cn.imx.dao.SpringProfileDAOImpl">
    		<property name="dataSource"><ref local="dataSource"/></property>
    	</bean>
    
    	<bean id="allocationCustomerDetailDAO" class="ca.cn.imx.dao.AllocationCustomerDetailDAOImpl">
    		<property name="dataSource"><ref local="dataSource"/></property>
    	</bean>
    
    	<bean id="springAllocationProfileExceptionDAO" class="ca.cn.imx.dao.SpringProfileExceptionDAOImpl">
    		<property name="dataSource"><ref local="dataSource"/></property>
    	</bean>
    </beans>
    Here's my class profile class implementation:

    Code:
    public class SpringProfileDAOImpl
    	extends JdbcDaoSupport
    	implements ProfileDAO, AllocationProfileSchema &#123;
    
    	private static AllocationProfileExceptionDAO allocationProfileExceptionDAO;
    	private static AllocationCustomerDetailDAO allocationCustomerDetailDAO;
    
    	static &#123;
    		ApplicationContextFactory.init&#40;
    			"/ca/cn/imx/appconfig/applicationContext.xml"&#41;;
    		allocationProfileExceptionDAO =
    			&#40;AllocationProfileExceptionDAO&#41; ApplicationContextFactory
    				.getApplicationContext&#40;&#41;
    				.getBean&#40;"springAllocationProfileExceptionDAO"&#41;;
    
    		allocationCustomerDetailDAO =
    			&#40;AllocationCustomerDetailDAO&#41; ApplicationContextFactory
    				.getApplicationContext&#40;&#41;
    				.getBean&#40;"allocationCustomerDetailDAO"&#41;;
    	&#125;
    
    	public List findProfileByLabel&#40;String label&#41; &#123;
    		ProfileMappingQuery profileQry =
    			new ProfileMappingQuery&#40;this.getDataSource&#40;&#41;&#41;;
    		Object&#91;&#93; parm = new Object&#91;1&#93;;
    		parm&#91;0&#93; = label;
    
    		List profiles = profileQry.execute&#40;parm&#41;;
    		if &#40;profiles.size&#40;&#41; > 0&#41;
    			return profiles;
    		else
    			return null;
    	&#125;
    
    	public AllocationProfileDO findProfileByPrimaryKey&#40;AllocationProfileDO profile&#41; &#123;
    		ProfileMappingQuery profileQry =
    			new ProfileMappingQuery&#40;this.getDataSource&#40;&#41;&#41;;
    		Object&#91;&#93; parm = new Object&#91;2&#93;;
    		parm&#91;0&#93; = profile.getLabel&#40;&#41;;
    		parm&#91;1&#93; = profile.getType&#40;&#41;;
    
    		List profiles = profileQry.execute&#40;parm&#41;;
    		if &#40;profiles.size&#40;&#41; > 0&#41; &#123;
    			AllocationProfileDO apdo = &#40;AllocationProfileDO&#41; profiles.get&#40;0&#41;;
    			apdo.setAllocationProfileCustomer&#40;
    				allocationCustomerDetailDAO.findCustomersByLabel&#40;
    					apdo.getLabel&#40;&#41;,
    					apdo.getType&#40;&#41;&#41;&#41;;
    
    			apdo.setAllocationProfileException&#40;
    				allocationProfileExceptionDAO.getExceptionsByProfile&#40;
    					apdo.getLabel&#40;&#41;,
    					apdo.getType&#40;&#41;&#41;&#41;;
    
    			return apdo;
    		&#125; else
    			return null;
    	&#125;
    
    	public void updateProfile&#40;AllocationProfileDO profile, UserDO user&#41;
    		throws DataAccessException &#123;
    		ProfileUpdateMappingQuery updateQuery =
    			new ProfileUpdateMappingQuery&#40;this.getDataSource&#40;&#41;&#41;;
    
    		Object&#91;&#93; parm = new Object&#91;10&#93;;
    		parm&#91;0&#93; = profile.getReleaseAllocation&#40;&#41;;
    		parm&#91;1&#93; = new Integer&#40;profile.getPriority&#40;&#41;&#41;;
    		parm&#91;2&#93; = new java.sql.Date&#40;profile.getEffective&#40;&#41;.getTime&#40;&#41;.getTime&#40;&#41;&#41;;
    		parm&#91;3&#93; = new java.sql.Date&#40;profile.getExpiry&#40;&#41;.getTime&#40;&#41;.getTime&#40;&#41;&#41;;
    		parm&#91;4&#93; = profile.getBusinessType&#40;&#41;;
    		parm&#91;5&#93; = new Integer&#40;profile.getIdRefText&#40;&#41;&#41;;
    		parm&#91;6&#93; = AuditSqlTool.PROGRAM_ID;
    		parm&#91;7&#93; = user.getUserId&#40;&#41;;
    		parm&#91;8&#93; = profile.getLabel&#40;&#41;;
    		parm&#91;9&#93; = profile.getType&#40;&#41;;
    
    		updateQuery.update&#40;parm&#41;;
    		updateCustomers&#40;profile, user&#41;;
    		updateExceptions&#40;profile, user&#41;;
    	&#125;
    
    	public void updateCustomers &#40;
    		AllocationProfileDO allocationProfile,
    		UserDO user&#41; throws DataAccessException &#123;
    		AllocationCustomerDetailDO tempCustomer = null;
    		AllocationCustomerDetailDO&#91;&#93; tempCustomerArray = null;
    		ArrayList originalCustomers = new ArrayList&#40;
    			&#40;List&#41; allocationCustomerDetailDAO.findCustomersByLabel&#40;
    				allocationProfile.getLabel&#40;&#41;,
    				allocationProfile.getType&#40;&#41;&#41;&#41;;
    				
    //			Object originalCustomers =
    //				&#40;List&#41; allocationCustomerDetailDAO.findCustomersByLabel&#40;
    //					allocationProfile.getLabel&#40;&#41;,
    //					allocationProfile.getType&#40;&#41;&#41;;
    					
    		System.out.println&#40;" ****** " +
    			"findCustomersByLabel returns Class" +
    			" of instance  "+originalCustomers.getClass&#40;&#41;&#41;;
    
    			/*  TEST */
    				if &#40;true&#41;&#123;
    					throw new DataAccessException&#40;""&#41;&#123;&#125;;			
    				&#125;
    			/*  TEST */
    
    		ArrayList newCustomers = 
    			new ArrayList&#40;allocationProfile.getAllocationProfileCustomer&#40;&#41;&#41;;
    		ArrayList&#91;&#93; sortedBo =
    			BOSortHelper.sortBO&#40;newCustomers, originalCustomers&#41;;
    
    		if &#40;sortedBo&#91;BOSortHelper.CREATE&#93;.size&#40;&#41; > 0&#41; &#123;
    			tempCustomerArray =
    				new AllocationCustomerDetailDO&#91;sortedBo&#91;BOSortHelper
    					.CREATE&#93;
    					.size&#40;&#41;&#93;;
    			Iterator iter = sortedBo&#91;BOSortHelper.CREATE&#93;.iterator&#40;&#41;;
    			
    			for &#40;int i = 0; iter.hasNext&#40;&#41;; i++&#41; &#123;
    				allocationCustomerDetailDAO.addCustomerDetail&#40;
    					&#40;AllocationCustomerDetailDO&#41; iter.next&#40;&#41;,
    					user&#41;;
    			&#125;
    		&#125;
    
    		if &#40;sortedBo&#91;BOSortHelper.DELETE&#93;.size&#40;&#41; > 0&#41; &#123;
    			tempCustomerArray =
    				new AllocationCustomerDetailDO&#91;sortedBo&#91;BOSortHelper
    					.DELETE&#93;
    					.size&#40;&#41;&#93;;
    			Iterator iter = sortedBo&#91;BOSortHelper.DELETE&#93;.iterator&#40;&#41;;
    			for &#40;int i = 0; iter.hasNext&#40;&#41;; i++&#41; &#123;
    				allocationCustomerDetailDAO.deleteCustomerDetail&#40;
    					&#40;AllocationCustomerDetailDO&#41; iter.next&#40;&#41;,
    					user&#41;;
    			&#125;
    		&#125;
    	&#125;
    
    	public void updateExceptions&#40;
    		AllocationProfileDO allocationProfile,
    		UserDO user&#41; throws DataAccessException&#123;
    		AllocationProfileExceptionDO tempException = null;
    		AllocationProfileExceptionDO&#91;&#93; tempExceptionArray = null;
    		ArrayList originalExceptions =new ArrayList&#40;allocationProfileExceptionDAO.getExceptionsByProfile&#40;
    				allocationProfile.getLabel&#40;&#41;,
    				allocationProfile.getType&#40;&#41;&#41;&#41;;
    		int highestId = -1;
    
    		for &#40;Iterator iter = originalExceptions.iterator&#40;&#41;; iter.hasNext&#40;&#41;;&#41; &#123;
    			tempException = &#40;AllocationProfileExceptionDO&#41; iter.next&#40;&#41;;
    			if &#40;tempException.getExceptionID&#40;&#41; > highestId&#41; &#123;
    				highestId = tempException.getExceptionID&#40;&#41;;
    			&#125;
    		&#125;
    		ArrayList&#91;&#93; sortedBo =
    			BOSortHelper.sortBO&#40;
    				&#40;ArrayList&#41; allocationProfile.getAllocationProfileException&#40;&#41;,
    				originalExceptions&#41;;
    
    		if &#40;sortedBo&#91;BOSortHelper.CREATE&#93;.size&#40;&#41; > 0&#41; &#123;
    			//tempExceptionArray = new AllocationProfileExceptionDO&#91;sortedBo&#91;BOSortHelper.CREATE&#93;.size&#40;&#41;&#93;;  
    			Iterator iter = sortedBo&#91;BOSortHelper.CREATE&#93;.iterator&#40;&#41;;
    			AllocationProfileExceptionDO exception = null;
    			for &#40;int i = 0; iter.hasNext&#40;&#41;; i++&#41; &#123;
    				exception = &#40;AllocationProfileExceptionDO&#41; iter.next&#40;&#41;;
    				exception.setExceptionID&#40;++highestId&#41;;
    				allocationProfileExceptionDAO.addProfileException&#40;
    					exception,
    					user&#41;;
    
    			&#125;
    		&#125;
    		if &#40;sortedBo&#91;BOSortHelper.DELETE&#93;.size&#40;&#41; > 0&#41; &#123;
    			//tempExceptionArray = new AllocationProfileExceptionDO&#91;sortedBo&#91;BOSortHelper.DELETE&#93;.size&#40;&#41;&#93;;  
    			Iterator iter = sortedBo&#91;BOSortHelper.DELETE&#93;.iterator&#40;&#41;;
    			for &#40;int i = 0; iter.hasNext&#40;&#41;; i++&#41; &#123;
    				allocationProfileExceptionDAO.addProfileException&#40;
    					&#40;AllocationProfileExceptionDO&#41; iter.next&#40;&#41;,
    					user&#41;;
    			&#125;
    		&#125;
    		if &#40;sortedBo&#91;BOSortHelper.UPDATE&#93;.size&#40;&#41; > 0&#41; &#123;
    			Iterator iter = sortedBo&#91;BOSortHelper.UPDATE&#93;.iterator&#40;&#41;;
    			for &#40;int i = 0; iter.hasNext&#40;&#41;; i++&#41; &#123;
    				allocationProfileExceptionDAO.updateProfileException&#40;
    					&#40;AllocationProfileExceptionDO&#41; iter.next&#40;&#41;,
    					user&#41;;
    			&#125;
    		&#125;
    	&#125;
    
    	public AllocationProfileDO addProfile&#40;
    		AllocationProfileDO profile,
    		UserDO user&#41;throws DataAccessException &#123;
    		ProfileMappingQuery profileQry =
    			new ProfileMappingQuery&#40;this.getDataSource&#40;&#41;&#41;;
    		Object&#91;&#93; parm = new Object&#91;2&#93;;
    		parm&#91;0&#93; = profile.getLabel&#40;&#41;;
    		parm&#91;1&#93; = profile.getType&#40;&#41;;
    		return null;
    	&#125;
    
    	private class ProfileMappingQuery extends MappingSqlQuery &#123;
    		public ProfileMappingQuery&#40;DataSource ds&#41; &#123;
    			super&#40;ds, SELECT_PROFILE_BY_PRIMARY_KEY&#41;;
    			super.declareParameter&#40;new SqlParameter&#40;COL_RALL_LBL, Types.VARCHAR&#41;&#41;;
    			super.declareParameter&#40;
    				new SqlParameter&#40;COL_RALL_LBL, Types.VARCHAR&#41;&#41;;
    			compile&#40;&#41;;
    		&#125;
    
    		public Object mapRow&#40;ResultSet rs, int rowNumber&#41; throws SQLException &#123;
    			AllocationProfileDO allocDO = new AllocationProfileDO&#40;&#41;;
    			allocDO.setLabel&#40;rs.getString&#40;COL_RALL_LBL&#41;.trim&#40;&#41;&#41;;
    			allocDO.setType&#40;rs.getString&#40;COL_RALL_TYPE_CD&#41;.trim&#40;&#41;&#41;;
    			allocDO.setEffective&#40;DateTool.getDate&#40;rs.getDate&#40;COL_RALL_EFF_DT&#41;&#41;&#41;;
    			allocDO.setExpiry&#40;DateTool.getDate&#40;rs.getDate&#40;COL_RALL_EXP_DT&#41;&#41;&#41;;
    			allocDO.setReleaseAllocation&#40;
    				rs.getString&#40;COL_RLSE_ALLC_IND&#41;.trim&#40;&#41;&#41;;
    			allocDO.setPriority&#40;rs.getInt&#40;COL_RALL_SERV_PRTY&#41;&#41;;
    			allocDO.setIdRefText&#40;rs.getInt&#40;COL_REF_TXT_ID&#41;&#41;;
    			allocDO.setBusinessType&#40;rs.getString&#40;COL_CUST_BUS_TYPE_CD&#41;&#41;;
    
    			return allocDO;
    		&#125;
    	&#125;
    	private class ProfileUpdateMappingQuery extends SqlUpdate &#123;
    		public ProfileUpdateMappingQuery&#40;DataSource ds&#41; &#123;
    			super&#40;ds, AllocationProfileSchema.UPDATE_ALLOCATION_PROFILE&#41;;
    
    			super.declareParameter&#40;
    				new SqlParameter&#40;
    					AllocationProfileSchema.COL_RLSE_ALLC_IND,
    					Types.VARCHAR&#41;&#41;;
    			super.declareParameter&#40;
    				new SqlParameter&#40;
    					AllocationProfileSchema.COL_RALL_SERV_PRTY,
    					Types.INTEGER&#41;&#41;;
    			super.declareParameter&#40;
    				new SqlParameter&#40;
    					AllocationProfileSchema.COL_RALL_EFF_DT,
    					Types.TIMESTAMP&#41;&#41;;
    			super.declareParameter&#40;
    				new SqlParameter&#40;
    					AllocationProfileSchema.COL_RALL_EXP_DT,
    					Types.TIMESTAMP&#41;&#41;;
    			super.declareParameter&#40;
    				new SqlParameter&#40;
    					AllocationProfileSchema.COL_CUST_BUS_TYPE_CD,
    					Types.VARCHAR&#41;&#41;;
    			super.declareParameter&#40;
    				new SqlParameter&#40;
    					AllocationProfileSchema.COL_REF_TXT_ID,
    					Types.NUMERIC&#41;&#41;;
    			super.declareParameter&#40;
    				new SqlParameter&#40;
    					AuditSqlTool.COL_AUDIT_UPDATE_PROGRAM,
    					Types.VARCHAR&#41;&#41;;
    			super.declareParameter&#40;
    				new SqlParameter&#40;
    					AuditSqlTool.COL_AUDIT_UPDATE_USER,
    					Types.VARCHAR&#41;&#41;;
    			super.declareParameter&#40;
    				new SqlParameter&#40;
    					AllocationProfileSchema.COL_RALL_LBL,
    					Types.VARCHAR&#41;&#41;;
    			super.declareParameter&#40;
    				new SqlParameter&#40;
    					AllocationProfileSchema.COL_RALL_TYPE_CD,
    					Types.VARCHAR&#41;&#41;;
    			compile&#40;&#41;;
    
    		&#125;
    
    		public int run&#40;Object&#91;&#93; params&#41; &#123;
    			return update&#40;params&#41;;
    		&#125;
    	&#125;
    
    &#125;
    in the updateCustomer method I intentionally throw a DataAccessException but it doesn't seem to work...

    How do I make it work using my proxy bean?

    TIA

  • #2
    Got it to work!

    Had to remove the references to the other daos in my proxy...

    Code:
    	<bean id="proxy"
    	    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    	    <property name="transactionManager"><ref bean="transactionManager"/></property>
       	    <property name="target"><ref local="springProfileDAO"/></property>	    
    	    <property name="transactionAttributes">
    	        <props>
    	            <prop key="add*">PROPAGATION_REQUIRED,-DataAccessException</prop>
    	            <prop key="update*">PROPAGATION_REQUIRED,-DataAccessException</prop>
    	            <prop key="delete*">PROPAGATION_REQUIRED,-DataAccessException</prop>	            
    	            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    	        </props>
    	    </property>
    	</bean>

    Comment

    Working...
    X