Transaction unable to Rollback
HI every one,
I am trying to implement Declarative Transaction Management using JDBC and my transaction is not rolling back when an exception occurs. Please check what i am doing is right or not.
My Interface contains methods and there implementation is in DImpl. I get bean from spring frame work in main of my implementation class and call function procWithStringOnly which uses OStoredProcedure to execute a stored procedure. After executing a stored procedure i call a method which throws Exception and ctach it in the same method that executes stored procedure. My Application Context says if the method throws UnsupportedOperationException the transaction should be rolled back, but it throws exception and at the same time transaction also gets committed. Please guide me where i am wrong i will be very thank ful.
Application Context.xml
<code>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="testClassTarget" class="Do.DImpl">
</bean>
<bean id="DInterface" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>
Do.DInterface
</value>
</list>
</property>
<property name="target">
<ref bean="testClassTarget"/>
</property>
<property name="transactionManager">
<ref bean="txManager"/>
</property>
<property name="transactionAttributeSource">
<ref bean="attributeSource"/>
</property>
</bean>
<bean id="attributeSource" class="org.springframework.transaction.interceptor .NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="procWithStringOnly">
PROPAGATION_REQUIRED, -UnsupportedOperationException
</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dDataSource " class="Test.DDataSource">
<property name="dataSource">
<ref local="dataSource"></ref>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource" destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.0.211:1521:ONCDB1 </value>
</property>
<property name="username">
<value>Test</value>
</property>
<property name="password">
<value>Test</value>
</property>
</bean>
</beans>
</code>
My Interface
<code>
package Do;
import org.springframework.context.ApplicationContext;
public interface DInterface
{
public void procWithStringOnly();
public void setAppContext(ApplicationContext apctx);
public void TestTransaction();
}
</code>
My Implementation Class
<code>
package Do;
import java.sql.Types;
import java.util.Hashtable;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlAp plicationContext;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import Test.OStoredProcedure;
public class DImpl implements DInterface
{
ApplicationContext applicationContext = null;
public void procWithStringOnly()
{
Hashtable procParamsTypes = new Hashtable();
Hashtable procParams = new Hashtable();
String strVal = "1»155»1»'1001100000'»'TALAL'»'AHMED'»''»'123 ROAD SIDE'»'07009'»'NJ'»'CEDAR GROVE'»''»'M'»'(434)324-3242'»''»'1966/02/16'»''»''»84430561»0»0»'2007/05/26'»3»'1210'»'1215'»''»''»84430561»'1444'»'3621'»' 1'»0»1";
String loginType = "1";
procParamsTypes.put("1", new SqlParameter("strVal", Types.VARCHAR));
procParamsTypes.put("2", new SqlParameter("loginType", Types.VARCHAR));
procParamsTypes.put("3", new SqlOutParameter("returnArray", Types.VARCHAR));
procParamsTypes.put("4", new SqlOutParameter("patientID", Types.VARCHAR));
procParamsTypes.put("5", new SqlOutParameter("slotAllocationID", Types.VARCHAR));
procParams.put("1", strVal);
procParams.put("2", loginType);
OStoredProcedure osp = new OStoredProcedure();
osp.executeStoredProcedure("SCHL_APPOINTMENTS.inse rt", procParamsTypes, procParams, applicationContext);
try
{
this.TestTransaction();
}
catch(UnsupportedOperationException uoe)
{
System.out.println("Please Rollback the transaction..........");
}
}
public void TestTransaction()
{
throw new UnsupportedOperationException();
}
public void setAppContext(ApplicationContext apctx)
{
this.applicationContext = apctx;
}
public static void main(String[] str1)
{
ApplicationContext appContext = new ClassPathXmlApplicationContext (
new String [] { "applicationContext.xml" } );
DInterface di = (DInterface) appContext.getBean("DInterface");
di.setAppContext(appContext);
di.procWithStringOnly();
}
}
</code>
My OStoredProcedure Class
<code>
package Test;
import java.sql.CallableStatement;
import java.sql.Types;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import oracle.jdbc.OracleTypes;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlAp plicationContext;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;
public class OStoredProcedure extends StoredProcedure
{
public OStoredProcedure()
{
}
public void executeStoredProcedure(String procName, Hashtable procParamsTypes, Hashtable procParams, ApplicationContext appContext)
{
Map inParams = new HashMap(2);
try
{
setDataSource(((DDataSource)appContext.getBean("dD ataSource")).getDataSoutce());
setSql(procName);
}
catch(Exception ex)
{
ex.printStackTrace();
}
for(int i=1; i<=procParamsTypes.size();i++)
{
try
{
Class cSqlOutParameter = Class.forName("org.springframework.jdbc.core.SqlOu tParameter");
Class cSqlParameter = Class.forName("org.springframework.jdbc.core.SqlPa rameter");
if(cSqlOutParameter.isInstance(procParamsTypes.get (i+"")))
{
System.out.println("in if");
declareParameter((SqlOutParameter) procParamsTypes.get(i+""));
}
else if(cSqlParameter.isInstance(procParamsTypes.get(i+ "")))
{
System.out.println("in else");
declareParameter((SqlParameter) procParamsTypes.get(i+""));
inParams.put(((SqlParameter) procParamsTypes.get(i+"")).getName(), procParams.get(i+"").toString());
}
}
catch(ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
}
compile();
Map outParams = execute(inParams);
if (outParams.size() > 0) {
String[] str1 = null;
System.out.println("Yes got return values");
} else {
System.out.println("have nothing");
}
}
}
</code>