File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JBoss/WildFly and the fly likes EJB2 CMP - Delayed DB Write Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "EJB2 CMP - Delayed DB Write" Watch "EJB2 CMP - Delayed DB Write" New topic

EJB2 CMP - Delayed DB Write

Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704

I have a number of EJB2 CMP beans with logic such as the follows:

In other words, create a widget with a specified ID then set additional attributes like name and description. Now, let me add additional information that widgetName is a required field in the database (cannot be null). In Geronimo, this wasn't a problem as the CMP flush/write was delayed until the end of the transaction. Now that I'm trying to run the same code in JBoss, I'm running into the problem that writes are sent to the database during the home.create() call and any missing fields (like Name) are throwing SQL Exceptions.

One solution would be to add name as part of the create method as follows:

The problem? I have hundreds of EJBs each with up to a dozen required fields. The fix would take a lot of time to implement. My question: is there a way I can get the same behavior as in geronimo, such that writes were delayed until after the create is run such as at the end of the transaction?

I have some other solutions such as remove the database constraint -or- modify the ejbCreate methods to insert blank data (like "" for strings or -1 for integers) but those seem like bad ideas to me. On the plus side they would save me from having to rewrite all the signatures.
[ August 15, 2008: Message edited by: Scott Selikoff ]

My Blog: Down Home Country Coding with Scott Selikoff
Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10101

Zero experience in EJB2 CMP, so what i mention here may not be of much use.

I found this in the JBoss Wiki InsertAfterEjbPostCreate and also from the JBoss CMP Engine documentation

[My Blog] [JavaRanch Journal]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704

Thanks Jaikiran,

The more I think about it the more I think Geronimo/OpenEJB was incorrect in allowing the INSERT's to occur after ejbCreate()/ejbPostCreate() is executed. In that regard there's no way to tell exactly when the objects are created in the database and its a time bomb waiting to go off for errors.

Just wish the original developers had foreseen this...
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16061

Convert it to EJB3 where delayed commit is required, since you can tag fields explicitly as not NULL.

You do need to expand the scope of the transaction, since if there are mandatory fields, it would be perilous in the extreme to have an eager commit kick in, regardless of the platform, and the default transaction scope for an EJB is per-method. Normally one would do this by setting up the entity bean from a session EJB.

Customer surveys are for companies who didn't pay proper attention to begin with.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704

Originally posted by Tim Holloway:
Convert it to EJB3

One giant conversion at a time. I figure moving from Geronimo to JBoss even with changing the ejbCreate methods is a 2-3 week project. Moving to EJB3 is well beyond that.

In the current situation, all the entity beans are set up by session beans, but in Geronimo/OpenEJB there was a notion that database writes wouldn't happen immediately, whereas JBoss has a different approach.
I agree. Here's the link:
subject: EJB2 CMP - Delayed DB Write