1

Accordinong to EJB 3.0 specification: While an instance is in a transaction, the instance must not attempt to use the resource-manager specific transaction demarcation API (e.g. it must not invoke the commit or rollback method on the java.sql.Connection interface or on the javax.jms.Session interface) In 13.3.3 of Specification. I tried one example - where in BEAN managed transaction I included java.sql.Connection.commit() - created Stateless bean in NetBeans as EE5, deployed on Glassfish 3.1 and container did not complain? Bean method updates the database without any errors in Glassfish log. Is this expected behavior?

Also, there is no such restriction on using java.sql.Connection.commit() for beans with container transaction managed transactions mentioned in specification. Thanks Branislav

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.*;
import javax.persistence.Transient;
import javax.sql.DataSource;
import javax.transaction.*;


/**
 *
 * @author bane
 */
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class MySession implements MySessionRemote {
    @Resource(name = "SAMPLE")
    private DataSource SAMPLE;

   //
   @Resource UserTransaction utx;

//gore je novi kod
    @Override
    public String getResult() {
        return "This is my Session Bean";
    }

      public void doSomething() {
        try {
            Connection conn = SAMPLE.getConnection();
            Statement stmt = conn.createStatement();
            String q = "select * from BOOK";
            String up = "update BOOK set PRICE = PRICE + 1";
           utx.begin();
           int num = stmt.executeUpdate(up);
           System.out.println("num: "+num);
           ResultSet rs = stmt.executeQuery(q);
           //is conn.commit() legal?
           conn.commit();
           String name = null;
           int price = 0;
           while (rs.next()) {
               name = rs.getString(2);
               price = rs.getInt(3);
               System.err.println(name+" , "+price);

           }

           utx.commit();
        } catch (SQLException ex) {
            Logger.getLogger(MySession.class.getName()).log(Level.SEVERE, null, ex);

        } catch (Exception ex) {
             Logger.getLogger(MySession.class.getName()).log(Level.SEVERE, null, ex);
        }
     }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

}
Roman C
  • 49,761
  • 33
  • 66
  • 176

0 Answers0