4 Replies Latest reply on Nov 6, 2007 1:45 AM by vickyk

    RollBack not working need help

    saulat78

      Hello

      I have a Stateless EJB, that is performing two transactions via DAOs,
      the first DAO inserts the record, then I am explicitly throwing exception in the second DAO and it fails to add record, then I expect the ut.rollback() method to roll back my first record but it is not doing so, can any one tell me what I am missing.
      Thanks
      Saulat

      @Stateless
      @TransactionManagement(TransactionManagementType.BEAN)
      public class DTCTestBean implements DTCTestRemote
      {
       @Resource SessionContext ctx;
      
       public void performTransaction(int param)
       {
      
       UserTransaction ut=null;
       Connection conn = null;
      
       try
       {
       InitialContext initCtx = new InitialContext(getProperties());
       DataSource ds = (javax.sql.DataSource)initCtx.lookup(DAOConstants.DB_VAM);
       conn = ds.getConnection();
       ut = ctx.getUserTransaction();
      
       // start the transaction
       ut.begin();
      
       FirstDAO dao = new FirstDAO();
       MyDTO dto = new MyDTO();
       dto.setA(123);
       int i = dao.insert(dto, conn); //performs the first transaction
      
      
       SecondDAO dao2 = new SecondDAO();
       SecondDTO dto2 = new SecondDTO();
       dto2.setB(456);
      
       int j = dao2.insert(dto2, conn) ; //explicitly throwing exception
      
       ut.commit();
       }
       catch(Exception e)
       {
       try
       {
       System.out.println("Attempting to roll back");
       ut.rollback(); // Y this dont roll back the first transaction
       System.out.println("Transaction roll back");
      
       }
       catch(Exception in)
       {
      
       in.printStackTrace();
       }
      
       }
       finally
       {
       try
       {
       if(conn != null)
       conn.close();
       }
       catch(Exception e)
       {
       System.out.println("Ignorable Exception " + e.toString() );
       }
       }
       }
      
       private Properties getProperties()
      {
       String urlName = "jnp://222.222.2.227:1099";
       Properties p = new Properties();
       p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
       p.put(Context.PROVIDER_URL, urlName);
       p.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
       return p;
      
       }
       }


        • 1. Re: RollBack not working need help
          jhalliday

          What is your data source configuration?

          • 2. Re: RollBack not working need help
            saulat78


            <datasources>
             <local-tx-datasource>
             <jndi-name>VAM</jndi-name>
             <use-java-context>false</use-java-context>
             <connection-url>jdbc:sqlserver://YCASHMTDEV:1433;databaseName=VAM</connection-url>
             <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
             <user-name>sa</user-name>
             <password>sa1234</password>
            
             <max-pool-size>100</max-pool-size>
             <min-pool-size>10</min-pool-size>
             <blocking-timeout-millis>5000</blocking-timeout-millis>
             <idle-timeout-minutes>15</idle-timeout-minutes>
            
             <!-- correspondinasdg type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
             <metadata>
             <type-mapping>mySQL</type-mapping>
             </metadata>
             <metadata>
             <type-mapping>MS SQLSERVER2005</type-mapping>
             </metadata>
             </local-tx-datasource>
            
            
            
            </datasources>


            Here it is, I am also using two more data sources in the same file but i only included that I am using.



            • 3. Re: RollBack not working need help
              saulat78

              I am getting a null pointer after setting

              <use-java-context>false</use-java-context> to true in my datasource-ds.xml


              java.lang.NullPointerException
               at DTCTestBean.performTransaction(DTCTestBean.java:94)
               at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)


              • 4. Re: RollBack not working need help
                vickyk

                 

                "saulat78" wrote:
                I am getting a null pointer after setting

                <use-java-context>false</use-java-context> to true in my datasource-ds.xml

                Here is what you get once you set use-java-context to false
                "use-java-context: If this is set to false the the datasource will be bound in the global JNDI context rather than the java: context."
                So the lookup should be now like this
                InitialContext context = new InitialContext();
                DataSource ds = (DataSource)context.lookup("VAM");
                

                I think you would have been using this line
                DataSource ds = (DataSource)context.lookup("java:/VAM");