Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Transaction unable to Rollback

  1. #1
    Join Date
    May 2007
    Posts
    9

    Default 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>

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    The problem here is that the transaction doesn't pass through the transaction boundary hence it can't be rolled back. Coupled with that internal method calls aren't proxied using Spring AOP, hence that can't trigger rollback either. You'd have to throw the exception from the DAO method to trigger rollback, call rollback programmatically or use AspectJ which can proxy internal calls.
    http://www.springframework.org/docs/...decl-explained
    http://www.springframework.org/docs/...ng-aop-proxies

    It really helps to post code in [code] [ /code] tags as well, it's soooo much easier to read.
    Last edited by karldmoore; Aug 30th, 2007 at 05:49 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3
    Join Date
    May 2007
    Posts
    9

    Default

    Hi karldmoore,
    Thanks for replying. Sir, i have a confusion which i want to make clear. I am trying to implement transaction using Declarative Transaction Management. I want to use jdk 1.4. Is it possible to use? After reading your reply i tried to implement the transaction as described in the document but it asks me to change jdk to 1.5 and i dont want it. I want to implement it in 1.4.

    Regards,
    Mohsin.

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Yes you can use 1.4, which part of it suggested that you couldn't?
    Last edited by karldmoore; Aug 30th, 2007 at 05:49 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  5. #5
    Join Date
    May 2007
    Posts
    9

    Default

    Following is my ApplicationContext.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    
    <beans>
    
    <!-- this is the service object that we want to make transactional -->
    <bean id="dInterface" class="Do.DImpl"/>
    
    <!-- the transactional advice (i.e. what 'happens'; see the <aop:advisor/> bean below) -->
      <tx:advice id="txAdvice" transaction-manager="txManager">
        <!-- the transactional semantics... -->
        <tx:attributes>
          <!-- other methods use the default transaction settings (see below) -->
          <tx:method name="procWithStringOnly"/>
        </tx:attributes>
      </tx:advice>
    
    <!-- ensure that the above transactional advice runs for any execution
          of an operation defined by the FooService interface -->
      <aop:config>
        <aop:pointcut id="dInterfaceOperation" expression="execution(* Do.DInterface.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="dInterfaceOperation"/>
      </aop:config>
    
    <bean id="dDataSource " class="Test.DDataSource">
       <property name="dataSource">
         <ref local="dataSource"></ref>
       </property>
    </bean>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" 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>
    
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
    </bean>
    </beans>

    Following is the exception i am getting
    Code:
    Exception in thread main
    org.springframework.beans.factory.BeanDefinitionStoreException: Parser configuration exception parsing XML from class path resource [applicationContext.xml]; nested exception is javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [org.apache.crimson.jaxp.DocumentBuilderFactoryImpl@13] does not support XML Schema. Are you running on Java 1.4 or below with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
    
    Caused by: javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [org.apache.crimson.jaxp.DocumentBuilderFactoryImpl@13] does not support XML Schema. Are you running on Java 1.4 or below with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
    
    	at org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory(DefaultDocumentLoader.java:101)
    
    	at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:70)
    
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:351)
    
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
    
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:280)
    
    	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:131)
    
    	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:147)
    
    	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:173)
    
    	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:112)
    
    	at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:80)
    
    	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:100)
    
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:298)
    
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:92)

  6. #6
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    There appears to be an option here "Upgrade to Apache Xerces (or Java 1.5) for full XSD support".
    Last edited by karldmoore; Aug 30th, 2007 at 05:49 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  7. #7
    Join Date
    Mar 2007
    Posts
    515

  8. #8
    Join Date
    May 2007
    Posts
    9

    Default

    Hi,
    First of all thanks Andrei Stefan and thanks a lot Karldmoore for your continuous help and support. Now, i have fixed the Xerces issue and i am facing another problem. I have prepard my ApplicationContext.xml as shown in the help on following url:-

    http://www.springframework.org/docs/...ansaction.html

    My ApplicationContext.xml is as follows:-
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    
    
    <!-- this is the service object that we want to make transactional -->
    <bean id="dInterface" class="Do.DImpl"/>
    
    <!-- the transactional advice (i.e. what 'happens'; see the <aop:advisor/> bean below) -->
      <tx:advice id="txAdvice" transaction-manager="txManager">
        <!-- the transactional semantics... -->
        <tx:attributes>
          <!-- other methods use the default transaction settings (see below) -->
          <tx:method name="procWithStringOnly"/>
        </tx:attributes>
      </tx:advice>
    
    <!-- ensure that the above transactional advice runs for any execution
          of an operation defined by the FooService interface -->
      <aop:config>
        <aop:pointcut id="dInterfaceOperation" expression="execution(* Do.DInterface.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="dInterfaceOperation"/>
      </aop:config>
    
    <bean id="dDataSource " class="Test.DDataSource">
       <property name="dataSource">
         <ref local="dataSource"></ref>
       </property>
    </bean>
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" 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>
    
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
    </bean>
    </beans>

    I am getting following exception:-
    Code:
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'DInterface' is defined
    
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java)
    
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedBeanDefinition(AbstractBeanFactory.java)
    
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:246)
    
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:163)
    
    	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:683)
    Please tell me where i am wrong. Thanks a lot once again.

    Mohsin

  9. #9
    Join Date
    Mar 2007
    Posts
    515

    Default

    I suppose that bean is missing :-) and indeed in not on your xml config file.

  10. #10
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I'd guess it's a case issue, there is a 'dInterface'.
    Last edited by karldmoore; Aug 30th, 2007 at 05:49 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

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