Performance will depend on how the dbms has been implemented but generally speaking a
select max would be very efficient as the primary key should be indexed (possibly
clustered) so the max value can be obtained directly from the index without even
referring to the table.
However, I'm still not convinced this is a safe thing to do. What happens if you have
two eb's being created at the same time, won't they both select the same max value,
add one leaving us with two identical primary keys?
>>> Ronald Perrella <[EMAIL PROTECTED]> 28/06/00 14:28:05 >>>
Isn't that going to have lousy performance? after all, the Max() function has
to do a table-scan to get that value?
-Ron
--- Santiago Gala <[EMAIL PROTECTED]> wrote:
>
>
> Phil Windley wrote:
>
> > Its faster to write, to be sure, but seems like its going to establish a
> > new connection for every creation event. If those events are a small
> > percentage of overall operations, it wouldn't be a problem, but I'm
> > envisioning a system that has large numbers of creations, so this would be
> > pretty expensive.
> >
> > Anyone else have ideas on getting a serial type (preferably from
> > Postgresql) to work with in CMP to automatically create the PK for the
> > bean?
> >
>
> What I'm doing is:
>
> Create a findGreatestId() method for each CMP bean. the where clause would be
> something like:
>
> WHERE id = (SELECT max(id) FROM table)
>
> Call it inside the ejbCreate method to get the biggest used id, increment it
> and
> assign it before returning. Warning: it will raise and exception, that must
> be
> caught, if the table is empty. In this case, set id to 1.
>
> It should work well, it is completely portable and it uses all the connection
> pooling and similar characteristics in Jonas.
>
> It is not very elegant, but we have seen no problems using it. If you require
> permanent unique ids, and you are removing objects, it is not for you, as the
> same
> id can be reused if the last object is created and then removed.
>
> >
> > --phil--
> >
> > On Fri, 23 Jun 2000 11:16:16 -0600 [EMAIL PROTECTED] writes
> > +--------------------
> > | I couldn't get the serial type to work with CMP either. We added a
> function
> > like the following in each of our entity beans to use automatically
> generated i
> > d's from postgres (mytable_id_seq is a SEQUENCE):
> > |
> > | private Integer _getNewID()
> > | throws SQLException
> > | {
> > | Connection conn=_dsource_.getConnection();
> > | String sql="select nextval('mytable_id_seq')";
> > | Statement stmt=conn.createStatement();
> > | ResultSet rs=stmt.executeQuery(sql);
> > | if(!rs.next())
> > | throw new SQLException("Unable to get next
> sequence.");
> > | Integer newid=new Integer(rs.getInt(1));
> > | stmt.close();
> > | conn.close();
> > | return newid;
> > | }
> > |
> > | I know this isn't an ideal solution, but it is faster than two beans..
> > |
> > | -Erik
> > |
> > |
> > | At 09:55 AM 6/23/00 -0600, you wrote:
> > | >
> > | >I use Postgresql as well, but I couldn't figure out how to get container
> > | >managed entity beans to automatically set the primary key from the
> SEQUENCE
> > | >data type. I ended up creating a session bean to grab sequence numbers
> > | >from the database each time I created an entity bean.
> > | >
> > | >Is there a better way? I'm all for it---creating two beans every time I
> nee
> > d
> > | >one is not my idea of a good way to spend time.
> > | >
> > | >--phil--
> > | >
> > | >On Thu, 15 Jun 2000 08:33:58 -0400 "John M. Jones" writes
> > | >+--------------------
> > | >| Does your database support serial fields? These fields generate a
> counter
> > | >| for each record added. There's a trick to getting the next id when
> calling
> > | >| create(), but it eliviates coding a unique id generator. I use
> PostgreSQL,
> > | >| and it has a serial data type.
> > | >|
> > | >| Hope this helps,
> > | >| John
> > | >|
> > | >| -----Original Message-----
> > | >| From: [EMAIL PROTECTED]
> > | >| [mailto:[EMAIL PROTECTED]]On Behalf Of Blasius Lofi
> > | >| Dewanto
> > | >| Sent: Thursday, June 15, 2000 5:13 AM
> > | >| To: [EMAIL PROTECTED]
> > | >| Subject: Automatic counter
> > | >|
> > | >|
> > | >| Hi all,
> > | >|
> > | >| I just wonder, whether someone can help me...
> > | >|
> > | >| How can I implement a counter or a unique id
> > | >| for my PrimaryKey in EJB? Because I don't want
> > | >| to take care about it in my EJB.
> > | >|
> > | >| Another question:
> > | >| When I quit from the EJBServer through the JonasAdmin,
> > | >| I always get this exception:
> > | >| Cannot unbind Datasources: java.lang.ClassCastException
> > | >| Is this normal?
> > | >|
> > | >| Thank's a lot!
> > | >| --
> > | >| ---------------------------------------------------
> > | >| Blasius Lofi Dewanto
> > | >| ---------------------------------------------------
> > | >| OpenUSS - Open Source University Support System
> > | >| http://openuss.sourceforge.net
> > | >| ---------------------------------------------------
> > | >|
> > | >|
> > | >| __________________________________________________________________
> > | >| Do You Yahoo!?
> > | >| Gesendet von Yahoo! Mail - http://mail.yahoo.de
> > | >| Yahoo! Auktionen - gleich ausprobieren - http://auktionen.yahoo.de
> > | >|
> > | >| ----
> > | >| 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".
> > | >|
> > | >| ----
> > | >| 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".
> > | >|
> > | >
> > | >----
> > | >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".
> > | >
> > | >
> > |
> > |
> >
> > ----
> > 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".
>
> ----
> 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".
__________________________________________________
Do You Yahoo!?
Get Yahoo! Mail - Free email you can access from anywhere!
http://mail.yahoo.com/
----
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".
**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.
This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.
www.mimesweeper.com
**********************************************************************
----
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".