2 Replies Latest reply on Dec 7, 2007 6:52 AM by guix

    How tom make a jdbc request inside Stateless EJB method ?

    noel.rocher

      Hi all,

      How can I make a JDBC request using the current transaction context in an EJB Session Stateless method ?

      Thanks

      Noel

        • 1. Re: How tom make a jdbc request inside Stateless EJB method

          Look up the datasource and ask for a connection.
          The jca wrapper will enroll the connection in the
          current transaction.

          Make sure you close the connection and any statements
          in a finally block to return them to the pool.

          Regards,
          Adrian

          • 2. Re: How tom make a jdbc request inside Stateless EJB method
            guix

             

            "adrian@jboss.org" wrote:
            Look up the datasource and ask for a connection.
            The jca wrapper will enroll the connection in the
            current transaction.

            Make sure you close the connection and any statements
            in a finally block to return them to the pool.

            Regards,
            Adrian


            Hi:
            I am trying this in Jboss 4.0.4 and it does not work:

            example:



            protected EJBContext myContext = null;
            public void setSessionContext (SessionContext ctx){
             myContext = ctx;
            }
            public void setUserData(String userId, String name, String roleId)
            throws RemoteException{
             try {
             UserLocal userLocal = UserFactoryLocal.getUser
            (userId); //Userlocal is an entity bean
             userLocal.setRoleId(roleId);
             Object object = myContext.lookup("java:/" + myDataSourceName); // Same datasoiurce name as the bean uses, it is a local datasource name
             Connection connection =
            ((DataSource)object).getConnection();
             connection.execute("update usuario set
            rol = '" + roleId + "' where usuario = '" + userId + "'";
            
             connection.close();
            
             userLocal.setUserName(name); //if this set fails there is no rollback performed to the previous execute
             }
             catch (Exception e) {
             e.printStackTrace();
             throw new EJBException(e);
             }
             }


            The example as you can see is functionalitywise, bogus, but I can see that if the code fails in setUserName then the new role appears set in the database eventhoug There is a EJB exception thrown....