aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "EJB2 CMP - Delayed DB Write" Watch "EJB2 CMP - Delayed DB Write" New topic
Author

EJB2 CMP - Delayed DB Write

Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

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
Marshal

Joined: Jul 20, 2005
Posts: 10202
    
166

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
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

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: 16137
    
  21

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
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3710
    
    5

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: http://aspose.com/file-tools
 
subject: EJB2 CMP - Delayed DB Write