0 Replies Latest reply on May 22, 2002 1:53 AM by Amar

    Transaction Problem -- urgent

    Amar Newbie

      Hello EveryBody,

      I am using JDBC-ODBC driver. and i have the maximum size of pool upto 10 connection.

      as i call the create method on my BMP bean it creats the records upto 10 records and after that it starts giving the error like

      javax.transaction.TransactionRolledbackException: INSERTING AN ALREADY EXISTING BEAN, ID = 10; nested exception is:
      java.lang.IllegalStateException: INSERTING AN ALREADY EXISTING BEAN, ID = 10
      java.lang.IllegalStateException: INSERTING AN ALREADY EXISTING BEAN, ID = 10


      i am using a method getNewId() to get the unique id of the bean. And after the 10 th call the getNewId() method starts returning the value only 10 (everytime).

      the deployment descriptor<ejb-jar>
      no description
      <display-name>Ejb1</display-name>
      <enterprise-beans>

      no description
      <display-name>cust_bean2</display-name>
      <ejb-name>cust_bean2</ejb-name>
      CustomerHome
      Customer
      <ejb-class>CustomerBean</ejb-class>
      <persistence-type>Bean</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      False
      <env-entry>
      <env-entry-name>debug</env-entry-name>
      <env-entry-type>java.lang.Integer</env-entry-type>
      <env-entry-value>2</env-entry-value>
      </env-entry>

      </enterprise-beans>
      <assembly-descriptor>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Home</method-intf>
      <method-name>findByState</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getAdd2</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getCity</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getAdd1</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getTele</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getContPer</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getFax</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Home</method-intf>
      <method-name>remove</method-name>
      <method-params>
      <method-param>java.lang.Object</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>setName</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getLstno</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Home</method-intf>
      <method-name>findByNameContaining</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>setDirect</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getName</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Home</method-intf>
      <method-name>remove</method-name>
      <method-params>
      <method-param>javax.ejb.Handle</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Home</method-intf>
      <method-name>findByPrimaryKey</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getAll</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Home</method-intf>
      <method-name>create</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>setLstno</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getState</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>setState</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getNewId</method-name>
      <method-params />

      <trans-attribute>Never</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getCstno</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>setContPer</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>remove</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Home</method-intf>
      <method-name>findByCity</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getDirect</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>setCstno</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>setDist</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>setFax</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>getDist</method-name>
      <method-params />

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>setAdd2</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>setCity</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>setAdd1</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      <container-transaction>

      <ejb-name>cust_bean2</ejb-name>
      <method-intf>Remote</method-intf>
      <method-name>setTele</method-name>
      <method-params>
      <method-param>java.lang.String</method-param>
      </method-params>

      <trans-attribute>Required</trans-attribute>
      </container-transaction>
      </assembly-descriptor>
      </ejb-jar>


      code of my BMP bean

      import javax.ejb.*;
      import java.rmi.*;
      import java.util.*;
      import javax.sql.*;
      import java.sql.*;
      import javax.naming.*;

      public class CustomerBean implements EntityBean
      {
      String id,name,add1,add2,city,state,contact_person,cst_no,lst_no,tele,fax,email,direct,dist;
      EntityContext entity_context;
      DataSource data_source;
      public void setEntityContext(EntityContext entity_context)
      {
      this.entity_context=entity_context;
      try
      {
      Context c=new InitialContext();
      data_source=(DataSource) c.lookup("java:/DefaultDS");
      }
      catch(NamingException e)
      {
      throw new EJBException("DataSource name lookup failed"+e);
      }
      }
      public void unsetEntityContext()
      {
      }

      public void ejbRemove()
      {
      }

      public void ejbLoad()
      {
      Connection con=null;
      try
      {
      con=data_source.getConnection();
      Statement st=con.createStatement();
      String sql=" SELECT CUST_CD,CUST_NAME,ADD1,ADD2,CITY,STATE,TELE,FAX,EMAIL,CONT_PER,DIST,CST_NO,LST_NO,DIRECT FROM CUST_MST WHERE CUST_CD='"+id+"'";
      ResultSet rs=st.executeQuery(sql);
      rs.next();
      id=rs.getString("CUST_CD");
      name=rs.getString("CUST_NAME");
      add1=rs.getString("ADD1");
      add2=rs.getString("ADD2");
      city=rs.getString("CITY");
      state=rs.getString("STATE");
      tele=rs.getString("TELE");
      fax=rs.getString("FAX");
      email=rs.getString("EMAIL");
      contact_person=rs.getString("CONT_PER");
      dist=rs.getString("DIST");
      cst_no=rs.getString("CST_NO");
      lst_no=rs.getString("LST_NO");
      direct=isNull(rs.getString("DIRECT"),"N");

      rs.close();
      st.close();
      con.close();

      }
      catch(SQLException e)
      {
      closecon(con);
      throw new EJBException(e);
      }
      }
      public void ejbStore()
      {
      Connection con=null;
      try
      {
      con=data_source.getConnection();
      Statement st=con.createStatement();
      String sql="UPDATE CUST_MST SET "+
      " CUST_CD='"+id+"',"+
      " CUST_NAME='"+name+"',"+
      " ADD1='"+add1+"',"+
      " ADD2 ='"+add2+"',"+
      " CITY ='"+city+"',"+
      " STATE='"+state+"',"+
      " TELE='"+tele+"',"+
      " FAX ='"+fax+"',"+
      " EMAIL ='"+email+"',"+
      " CONT_PER ='"+contact_person+"',"+
      " DIST ='"+dist+"',"+
      " CST_NO ='"+cst_no+"',"+
      " LST_NO ='"+lst_no+"',"+
      " DIRECT='"+direct+"' "+
      " WHERE CUST_CD='"+id+"'";
      st.executeUpdate(sql);
      st.close();
      con.close();
      }
      catch(SQLException e)
      {
      closecon(con);
      throw new EJBException(e);
      }
      }
      public void ejbActivate()
      {
      id=(String)entity_context.getPrimaryKey();
      }
      public void ejbPassivate()
      {

      }
      public String ejbFindByPrimaryKey(String id)
      throws FinderException
      {
      Connection con=null;
      Statement st;
      ResultSet rs;
      try
      {
      con=data_source.getConnection();
      st=con.createStatement();
      rs=st.executeQuery("SELECT CUST_CD,CUST_NAME,ADD1,ADD2,CITY, "+
      " STATE,CONT_PER,CST_NO,LST_NO,TELE,FAX,EMAIL,"+
      " DIST,DIRECT FROM CUST_MST WHERE CUST_CD='"+id+"'");
      if (rs.next())
      {
      this.id=rs.getString("CUST_CD");
      this.name=rs.getString("CUST_NAME");
      this.add1=rs.getString("ADD1");
      this.add2=rs.getString("ADD2");
      this.city=rs.getString("CITY");
      this.state=rs.getString("STATE");
      this.contact_person=rs.getString("CONT_PER");
      this.cst_no=rs.getString("CST_NO");
      this.lst_no=rs.getString("LST_NO");
      this.tele=rs.getString("TELE");
      this.fax=rs.getString("FAX");
      this.email=rs.getString("EMAIL");
      this.dist=rs.getString("DIST");
      this.direct=isNull(rs.getString("DIRECT"),"N");
      rs.close();
      st.close();
      con.close();
      return id;
      }

      else throw new ObjectNotFoundException("No Customer with id="+id);

      }catch(SQLException e)
      {
      closecon(con);
      throw new EJBException(e);
      }

      }

      public Collection ejbFindByNameContaining(String str)
      throws FinderException
      {
      Connection con=null;
      ArrayList ary=null;
      try
      {
      con=data_source.getConnection();
      Statement st=con.createStatement();
      ResultSet rs=st.executeQuery("SELECT * FROM CUST_MST WHERE CUST_NAME LIKE '%"+str+"%'");
      ary=new ArrayList();
      while (rs.next())
      {
      ary.add(rs.getString("CUST_CD"));
      }
      rs.close();
      st.close();
      con.close();


      }catch(SQLException e)
      {
      closecon(con);
      throw new EJBException(e);
      }
      return ary;
      }
      public Collection ejbFindByCity(String str)
      throws FinderException
      {
      Connection con=null;
      try
      {
      con=data_source.getConnection();
      Statement st=con.createStatement();
      ResultSet rs=st.executeQuery("SELECT * FROM CUST_MST WHERE CITY='"+str+"'");
      ArrayList ary=new ArrayList();
      while (rs.next())
      {
      ary.add(rs.getString("CUST_CD"));
      }
      rs.close();
      st.close();
      con.close();
      return ary;

      }catch(SQLException e)
      {
      closecon(con);
      throw new FinderException(e.toString());
      }


      }

      public Collection ejbFindByState(String str)
      throws FinderException
      {
      Connection con=null;
      try
      {
      con=data_source.getConnection();
      Statement st=con.createStatement();
      ResultSet rs=st.executeQuery("SELECT * FROM CUST_MST WHERE STATE='"+str+"'");
      ArrayList ary=new ArrayList();
      while (rs.next())
      {
      ary.add(rs.getString("CUST_CD"));
      }
      rs.close();
      st.close();
      con.close();
      return ary;

      }catch(SQLException e)
      {
      closecon(con);
      throw new FinderException(e.toString());
      }


      }
      public Collection getAll()
      throws RemoteException
      {
      Connection con=null;
      try
      {
      con=data_source.getConnection();
      Statement st=con.createStatement();
      ResultSet rs=st.executeQuery("SELECT * FROM CUST_MST ");
      ArrayList ary=new ArrayList();
      while (rs.next())
      {
      ary.add(rs.getString("CUST_NAME"));
      }
      rs.close();
      st.close();
      con.close();
      return ary;

      }catch(SQLException e)
      {
      closecon(con);
      throw new RemoteException(e.toString());
      }

      }
      public void ejbPostCreate(String id, String name,String add1, String add2,String city,
      String state,String contactperson,String cst_no, String lst_no,String tele,String fax,
      String email,String dist,String direct)
      {

      }
      public String ejbCreate(String id, String name,String add1, String add2,String city,
      String state,String contactperson,String cst_no, String lst_no,String tele,String fax,
      String email,String dist,String direct) throws CreateException
      {
      id=id.toUpperCase();
      name=name.toUpperCase();
      add1=add1.toUpperCase();
      add2=add2.toUpperCase();
      city=city.toUpperCase();
      state=state.toUpperCase();
      contactperson=contactperson.toUpperCase();
      cst_no=cst_no.toUpperCase();
      lst_no=lst_no.toUpperCase();
      tele=tele.toUpperCase();
      fax=fax.toUpperCase();
      dist=dist.toUpperCase();
      email=email;
      direct=direct.toUpperCase();
      Connection con=null;

      try
      {
      con=data_source.getConnection();
      Statement st=con.createStatement();

      String sql=" INSERT INTO CUST_MST ("+
      " CUST_CD,CUST_NAME,ADD1,ADD2,CITY,STATE,CONT_PER,CST_NO,LST_NO,TELE,FAX,EMAIL,DIST,DIRECT) "+
      " VALUES('"+id+"','"+name+"','"+add1+"','"+add2+"','"+city+"','"+state+"','"+contactperson+"','"+cst_no+"','"+lst_no+"','"+tele+"','"+fax+"','"+email+"','"+dist+"','"+direct+"') ";

      st.executeUpdate(sql);
      st.close();
      con.close();
      System.out.println(sql);
      this.id=id.toUpperCase();
      this.name=name.toUpperCase();
      this.add1=add1.toUpperCase();
      this.add2=add2.toUpperCase();
      this.city=city.toUpperCase();
      this.state=state.toUpperCase();
      this.contact_person=contactperson.toUpperCase();
      this.cst_no=cst_no.toUpperCase();
      this.lst_no=lst_no.toUpperCase();
      this.tele=tele.toUpperCase();
      this.fax=fax.toUpperCase();
      this.email=email;
      this.dist=dist.toUpperCase();
      this.direct=direct.toUpperCase();
      return id;


      }catch(SQLException e)
      {
      System.out.println(e);
      closecon(con);
      throw new EJBException(e);
      }

      }
      public Long getNewId() throws RemoteException
      {
      Connection con=null;
      Long l=null;
      try
      {
      con=data_source.getConnection();
      Statement st=con.createStatement();
      ResultSet rs=st.executeQuery("SELECT MAX(CUST_CD) FROM CUST_MST ");
      rs.next();

      l=new Long(rs.getLong(1)+1);

      rs.close();
      st.close();
      con.close();

      }
      catch(SQLException e)
      {
      closecon(con);
      throw new RemoteException(e.toString());
      }
      return l;
      }

      private String isNull(String str,String str1)
      {
      if (str==null || str.equalsIgnoreCase("null")) str=str1;
      else str=str.trim().toUpperCase();
      return str;
      }
      private String isNull(String str)
      {
      return isNull(str,"");
      }
      private void closecon(Connection con)
      {
      try
      {
      con.close();
      }catch(Exception e)
      {
      System.out.println(e);
      }
      }

      }


      Please reply asap
      Amar