Don't know Informix, but i imagine it has you can also alter manually your
sequence generator.
So, let your Bean class set the id itself, using a static int property, and
have this property being sync to the sequence generator regulary. Here's the
code i used for postreSQL :
 /**
   * Gets the next account ID.
   */
  private int getNewId() throws Exception {
    if (verboseMode)
    System.out.println("getNewAccountId(); lastKey: " + lastKey + " maxKey: " + 
maxKey);

    if (lastKey >= maxKey) {
      // Go to database and set new values for lastKey and maxKey
      if (verboseMode)
      System.out.println("lastKey >= maxKey: getting new range of accountIds");
      Connection con=null;
      PreparedStatement ps=null;
      try {
         con = dataSource.getConnection();
         ps  = con.prepareStatement("select setval('taxon_id_seq', 
nextval('taxon_id_seq')+?);");
        ps.setInt(1, keyRange-1);
        ResultSet rs = ps.executeQuery();
        if (rs==null) System.out.println("null");
        if ((rs != null) && (rs.next())) {
          maxKey  = rs.getInt(1);
          lastKey = maxKey - keyRange;
        } else {
          throw new CreateException ("ejbCreate: sequence failed to return a value");
        }
      }
      catch (Exception ex) {
        throw new RemoteException (ex.getMessage());
      }
      finally {
        try { ps.close();
          con.close();
      } catch (SQLException ex) {};
      }
    }
    return ++lastKey;
  }
If you can't use sequence generator, you can also use a table with a field for
each of your EJB, and increment it manually.

Hope this helps

 Le jeu, 30 mar 2000, vous avez �crit :
> Sorry, I accidentally sent this question to jonas-team instead of
> jonas-users.
> 
> Here is my question:
> 
> I'm using INFORMIX-OnLine with Jonas and want to make use of primary key
> values for my CMP-Beans generated by the INFORMIX-Server. This can be done
> with a SERIAL column in INFORMIX. I already know that there is no ANSI SQL
> way to do this, but as I just use INFORMIX I don't care much about
> portability in this respect. I already tried to do the following:
> 
> - in the method ejbCreate() I set the primary key field (an Integer) to 0.
> This tells INFORMIX to generate a value that is unique in the table an can
> be used as a primary key for the table. This works fine so far, but I need
> to know which number the server generated for the row just inserted.
> 
> - in the method ejbPostCreate() I execute the following code to get the
> primary-key value:
> 
>     try {
>       conn = ... (utility to get a Connection)
>       stmt = conn.createStatement();
>       ResultSet res = stmt.executeQuery( "select DBINFO( 'sqlca.sqlerrd1' )
> from " + Dt10001EB.TABLENAME );
>       if( res.next() == false ) {
>         throw new CreateException( "Can't determine primary key" );
>       }
>       int resId = res.getInt( 1 );
>       id = new Integer( resId );
>       Dt10001EBPK pk = (Dt10001EBPK)entityContext.getPrimaryKey();
>       pk.id = id;
>       System.out.println( "====> serial = " + resId );
>     }
>     catch( SQLException ex ) {
>       throw new CreateException( ex.toString() );
>     }
>     catch( javax.naming.NamingException ex2 ) {
>       throw new CreateException( ex2.toString() );
>     }
>     finally {
>       try {
>         if( stmt != null )
>           stmt.close();
>         if( conn != null )
>           conn.close();
>       }
>       catch( SQLException ex3 ) {
>       }
>     }
> 
> This code works quite well. But when I try to access fields from this bean
> within the same transaction I notice that the bean is loaded from the
> database again. This does not happen, when i create a bean with a primary
> key != 0. That's why I fear, that Jonas caches the primary key 0 before
> ejbPostCreate() is called and wonder if this solution would work in all
> circumstances or if I would end up with inconsistent EntityBeans in my
> container.
> 
> Can someone please clear this up? Is there an easier way to use INFORMIX
> SERIALS?
> 
> Best Regards,
> Albert
-- 
Guillaume Rousse

Iremia - Universit� de la R�union
15 avenue Ren� Cassin, BP 7151
97715 Saint Denis, messagerie cedex 9
Tel:0262938330 Fax:0262938260 ICQ:10756815

And now, some words for our sponsor :
Explosives, guns, assassination, conspiracy, primers, detonators, Kakadan,
initiators, main charge, nuclear charges, ambush, sniping, motorcade, IRS,
ETA, FARC, Cuba, Castro, Che Guevara, revolution, ATTAC, BATF, jtf-6...

----
To unsubscribe, send email to [EMAIL PROTECTED] and
include in the body of the message "unsubscribe jonas-users".
For general help, send email to [EMAIL PROTECTED] and
include in the body of the message "help".

Reply via email to