• Post Reply Bookmark Topic Watch Topic
  • New Topic

CMP insert problem  RSS feed

 
Rusty Enisin
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue of creating a new PK when inserting a new record seems to have been beaten to death. I have read several postings but don't really seem to get it.

For creating the PK, our database does that. No matter what PK value I give the database, it will ignore what I say and put in the next available index number. I cannot change that.

From what I understand (and that could be the problem right there...) you need to get the PK value first and then set it in the bean. So how do I get the number from the database and give that to CMP? Do I really have to query the database myself to get the next available PK index? I saw that in another post, but that seems rediculous in a CMP where I am trying to ignore JDBC as much as possible.

This approach would seem to lend itself to some concurrency problems. Two or more beans could get the next available index at the same time and then you have really big problems.

You could use an index generator, but my database will ignore that anyway.

Another idea I had was to create a blank record with a JDBC connection (if you had to do that), get the PK, and then I could continue as an update. But then again, why do that? I could just insert the data at the same time I am creating the record with the JDBC connection.

All things considering, creating a record with CMP seems useless to me. Am I missing something? Is there a way to do this?
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rusty,

As far as I know the container should be able to automatically generate unique primary keys (if I remember correctly this is required by the EJB spec). You might check your container documentation though. Weblogic for example has several strategies for that, including using oracle sequences or vendor-neutral named sequence tables.
Regards.
 
Rusty Enisin
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

I just tried letting the application server auto generate the pk. That did not work either.

I am working with an Informix database. I have looked all over the net trying to find somebody that has had the same problem. I have found a few solutions, but none of them include being able to just get the next index and insert.

Here are the ones I have found:


1. Use a static field to track the pk. This could work, but could get out of sync really fast if this bean is not the only way to access the db. The good thing is that you could recover by trying again and again until it worked, incrementing the static field each time. You would have to use jdbc anyway just to set the value the first time because a server restart of any kind would start you back at 0.

2. Use an external pk generator. I am not too fond of this. This could get out of sync really fast too. Concurrency is still an issue.

3. Use an exteranl tool that looks up the next available pk for you. This too seems to lend itself to problems when several beans look up at the same time. Then they all try to create the same pk on top of eash other. But you could try again and again. In the end this seems to have the same result as the first option.

4. Let the application server handle it. Some application servers support pk generation. But there are two kinds. Some will generate a pk. Others will let the database generate it. Sun's application server only supports the first kind and will not let the database generate it. This is a big thumbs down for Sun's application server. I tried to get it to work by using the application server's generated pk. But that did not work. Bring on WebLogic!

5. Use another table to keep track of the next available pk value. This, strangely enough, seems to be the best option. Yes, it is another table. And another bean. But it could all be handled without jdbc connections. It is persistent with server restarts. It would seem to reduce the chances of concurrent access. At this point. This is the best option I have found.


I have also found that the informix driver includes a class in the com.informix.jdbc package that is an implementation of the java.sql.Statement interface. It contains two methods called getSerial() and getSerial8(). These can be used to obtain the index. Now if there was just a way to call the method from my bean without JDBC!

arg.... I sure hope this is fixed in EJB 3.0!
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for posting your researches, they are really interesting.
Regards.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!