2 Replies Latest reply on Feb 10, 2005 4:34 PM by Daniel Georges

    Servlet and UserTransaction

    Daniel Georges Newbie

      Hi!

      I tried to use a Transaction in a servlet, but unfortunately the transaction doesn't affect my SQL-executions. It seems that they are not bound to this transaction.

      database = DataBase.getDatabase();
      connection = database.getConnection(m_data.getStringAttribute("datasource")); // DataSourc lookup
      
      InitialContext jndiContext = new InitialContext();
      transaction = (javax.transaction.UserTransaction) jndiContext.lookup("java:comp/UserTransaction");
      // Transaktion beginnen
      transaction.begin(); //
      
       // Deleteproposalindex
      final String DELETEPROPOSALINDEX = "DELETE FROM PROPOSALINDEX WHERE CLIENT=? AND DIVISION=? AND PROPOSALID=? AND ACCOUNTSOURCE=? AND ACCOUNTNUMBER=?";
      stmtDeleteProposalindex = connection.prepareStatement(_DELETEPROPOSALINDEX ,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); // Statement initialisieren
      stmtDeleteProposalindex.setString(1, m_data.getStringAttribute("client"));
      stmtDeleteProposalindex.setString(2, m_data.getStringAttribute("division"));
      stmtDeleteProposalindex.setString(3, m_data.getStringAttribute("proposalid"));
      stmtDeleteProposalindex.setString(4, accountSource);
      stmtDeleteProposalindex.setString(5, allAccounts.getString("accountnumber"));
      stmtDeleteProposalindex.executeUpdate();
      stmtDeleteProposalindex.close();
      transaction.rollback();
      


      The problem is, that this proposalindex will be deleted every time even if I rollback the transaction. It seems that every select is autocommited.

      What am I doing wrong?

      Any help/hint is appreciated.

      regards
      Dan

        • 1. Re: Servlet and UserTransaction
          Scott Stark Master

          You need to use a DataSource that has been deployed through the jboss jca layer. I have no idea what the DataBase.getDatabase() call does.

          • 2. Re: Servlet and UserTransaction
            Daniel Georges Newbie

            Hi!

            Database.getConnection() does this

             public Connection getConnection(String _dataSource) throws Exception{
             try {
             DataSource source = (DataSource) XIfsCache.get(_dataSource);
             if (source == null) {
             // Get a naming context
             InitialContext jndiContext = getInitialContext();
             source = (DataSource) jndiContext.lookup(_dataSource);
             XIfsCache.put(_dataSource, source);
             }
             return source.getConnection();
             }
             catch (Exception ex) {
             throw ex;
             }
             }
            


            Is this okay? Inside the Session Beans or EJBs this works very well.
            Only the Servlet doesn't want to work like it should.

            Regards
            Dan