5 Replies Latest reply on Mar 31, 2008 8:07 AM by Jonathan Halliday

    JBoss TS with PostgreSQL without JNDI

    Sebastian Hennebrueder Newbie

      Hello,
      I was actually trying to integrate Hibernate, TreeCache and JBoss TS in a standalone application.

      I found the blog post http://blogs.jboss.com/blog/gzamarreno/?permalink=No_Need_To_Hacer_De_Celestina_Any_More.txt
      and evaluated the example at http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossCacheHibernateTransactionsStandaloneExample.

      This example application used allready JBossTS with TreeCache but used a plain JDBCTransaction in Hibernate.
      The missing things are only a special kind of JTSTransaction and JtsTransaction Manager to allow Hibernate integration with JTS. I developed them on the base of the JTA implementations and just used JTS specific code where required - only a couple of changes. But this is not yet working as the JbossTs transaction was not bound to the JDBC connection.

      The problem, I am having know is a pure JBossTS problem. I would like to use PostgreSQL as database but I do not manage to bind the driver to the JBoss Ts transaction. The Ts commit does not commit the database.

      I simplified the code and have a pure JDBC snippet now. It is not working, the connection is always Null.

      The programmers guide of JbossTs 4.2.3 is not very clear on DynamicClass and does not perfectly correspond to the JBoss TS source.

      Here a couple of questions:
      Can I integrate a simple JDBC driver?
      Do I need a dynamicClass implementation, if my database is not supported out of the box?
      Do I have to use a XADatasource or is this only required, if I really want to go for a distributed transaction?
      Do I have to use JNDI for the JDBC driver?
      Can somebody give me a tip on how to make my code snipped work?

      Best Regards

      Sebastian Hennebrueder

      public static void main(String[] args) {
       System.setProperty("jdbc.drivers", "org.postgresql.Driver");
       Properties dbProps = new Properties();
       dbProps.setProperty(TransactionalDriver.userName, "postgres");
       dbProps.setProperty(TransactionalDriver.password, "p");
       String url = "jdbc:postgresql://localhost:5432/learninghibernate";
      
       TransactionalDriver arjunaJDBC2Driver = new TransactionalDriver();
       try {
      
       Connection connection = arjunaJDBC2Driver
       .connect(url, dbProps);
      
       Random r = new Random();
       int id = r.nextInt();
      
       System.out.println("conn="+connection);
       javax.transaction.UserTransaction ut = UserTransaction.userTransaction();
       try {
       ut.begin();
       connection
       .prepareStatement("insert into plant (id, name, growing) values (" + id + ",'abc',true)").execute();
       ut.commit();
       } catch (NotSupportedException e) {
       e.printStackTrace();
       } catch (SystemException e) {
       e.printStackTrace();
       } catch (RollbackException e) {
       e.printStackTrace();
       } catch (HeuristicMixedException e) {
       e.printStackTrace();
       } catch (HeuristicRollbackException e) {
       e.printStackTrace();
       }
       } catch (SQLException e) {
       e.printStackTrace();
       }
      
       }


        • 1. Re: JBoss TS with PostgreSQL without JNDI
          Jonathan Halliday Master

          Ok, I'm confused. Firstly why do you need JTS rather than JTA? Secondly, why are you interested in using raw JDBC? I thought you were using hibernate, in which case you should never need to manage or even see the db connection in your code.

          • 2. Re: JBoss TS with PostgreSQL without JNDI
            Sebastian Hennebrueder Newbie

            Hello,

            it is probably me being confused and not precise.
            I probably meant JTA. I would like to use JTA inside of a Java application or in web application deployed to Tomcat.
            I created the JDBC snipped to isolate the problem. Once this is running, I will try to make Hibernate, TreeCache and JBossTs play together.

            Best Regards
            Sebastian

            • 3. Re: JBoss TS with PostgreSQL without JNDI
              Jonathan Halliday Master

              Hmm, ok. You would need to either write a dynamic class for the PostgreSQL driver, or use JNDI. I recommend the latter.

              • 4. Re: JBoss TS with PostgreSQL without JNDI
                Sebastian Hennebrueder Newbie

                Hello,

                I have a first working solution without JNDI and will try to create a JNDI version running in Tomcat as well.

                I get a EOF message after the application ends. Is there any way to tell JBossTS to close the connection or is it actually expecting a connection pool and therefor not closing at all?

                My JBossTs version 4.2.3
                The dynamic class: (works only for a local database on port 5432)

                public class PostgreSqlDynamicClass implements DynamicClass {
                 private static Logger log = Logger.getLogger(PostgreSqlDynamicClass.class);
                
                 public XADataSource getDataSource(String dbName) throws SQLException {
                 return getDataSource(dbName, true);
                 }
                
                 public XADataSource getDataSource(String dbName, boolean create) throws SQLException {
                 log.debug("Getting datasource for db: " + dbName);
                
                 PGXADataSource source = new PGXADataSource();
                 source.setDatabaseName(dbName);
                 return source;
                 }
                
                 public void shutdownDataSource(XADataSource ds) throws SQLException {
                 log.debug("shutting down datasource: " + ds);
                 }
                }
                
                


                My Test:
                public class JdbcTest {
                 public static void main(String[] args) {
                
                 Properties dbProps = new Properties();
                 dbProps.setProperty(TransactionalDriver.userName, "postgres");
                 dbProps.setProperty(TransactionalDriver.password, "p");
                 dbProps.setProperty(TransactionalDriver.dynamicClass, "org.hibernate.transaction.PostgreSqlDynamicClass");
                 String url = "jdbc:arjuna:learninghibernate";
                
                 TransactionalDriver arjunaJDBC2Driver = new TransactionalDriver();
                 try {
                
                 Connection connection = arjunaJDBC2Driver
                 .connect(url, dbProps);
                
                 javax.transaction.UserTransaction ut = UserTransaction.userTransaction();
                 try {
                 ut.begin();
                 Random r = new Random();
                 int id = r.nextInt();
                 PreparedStatement statement = connection
                 .prepareStatement("insert into plant (id, name, growing) values (" + id + ",'abc',true)");
                 statement.execute();
                 statement.close();
                
                 ut.commit();
                
                 } catch (NotSupportedException e) {
                 e.printStackTrace();
                 } catch (SystemException e) {
                 e.printStackTrace();
                 } catch (RollbackException e) {
                 e.printStackTrace();
                 } catch (HeuristicMixedException e) {
                 e.printStackTrace();
                 } catch (HeuristicRollbackException e) {
                 e.printStackTrace();
                 }
                 } catch (SQLException e) {
                 e.printStackTrace();
                 }
                
                 }
                }
                


                • 5. Re: JBoss TS with PostgreSQL without JNDI
                  Jonathan Halliday Master

                  > I get a EOF message after the application ends. Is there any way to tell JBossTS to close the connection

                  Umm, call conn.close() in your code?