0 Replies Latest reply: Aug 15, 2013 6:52 AM by Valery_Cherepanov RSS

    UserTransaction in a standalone app

    Valery_Cherepanov

      Hello,
      I'm trying to understand if it's possible to use app server's JTA from a standalone application. I'm testing with JBoss-5.1.0 using all types of datasources (xa-datasource, local-tx-datasource, no-tx-datasource). The code below runs without any exceptions, but update is not actually being committed (I do not see from a different session that changes are applied). Running that code for the second time ends up with Duplicate PKs Oracle exception, meaning that session holds the change.


      public static void main(String[] args) throws Exception {
        
      Hashtable ht = new Hashtable();
        ht
      .put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
        ht
      .put(Context.PROVIDER_URL, "jnp://localhost:1099");
        ht
      .put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
        
      Context ctx = new InitialContext(ht);

        javax
      .sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("hr_local_tx");
        
      Connection connection = ds.getConnection();
        connection
      .setAutoCommit(false);

        
      UserTransaction tx = (UserTransaction) ctx.lookup("UserTransaction");
        
      try {
        tx
      .begin();
        
      Statement statement = connection.createStatement();
        statement
      .executeUpdate("insert into JOBS values('A', 'A', 10000, 50000)");
        statement
      .close();
        tx
      .commit();
        
      } catch (SQLException e) {
        tx
      .rollback();
        e
      .printStackTrace();
        
      } finally {
        connection
      .close();
        
      }
      }