1

EJB 3.0 仕様によると、インスタンスがトランザクション内にある間、インスタンスはリソース マネージャ固有のトランザクション境界 API を使用しようとしてはなりません (たとえば、java.sql.Connection インターフェイスまたはjavax.jms.Session インターフェイス) 仕様の 13.3.3。1 つの例を試してみました - BEAN 管理のトランザクションに java.sql.Connection.commit() を含めました - NetBeans でステートレス Bean を EE5 として作成し、Glassfish 3.1 にデプロイしましたが、コンテナは文句を言いませんでしたか? Bean メソッドは、Glassfish ログにエラーなしでデータベースを更新します。これは予想される動作ですか?

また、仕様に記載されているコンテナー トランザクション管理トランザクションを持つ Bean に対して java.sql.Connection.commit() を使用する場合、そのような制限はありません。ありがとうブラニスラフ

/*
 * 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")

}
4

0 に答える 0