permaculture playing cards
The moose likes Object Relational Mapping and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark ""Upsert" using Container Managed Persistence?" Watch ""Upsert" using Container Managed Persistence?" New topic

"Upsert" using Container Managed Persistence?

Lyn Pierce

Joined: Nov 10, 2011
Posts: 3
Hi, all -

I'm a little confused about what concurrency control, if any, is built into EJB Container Managed Persistence. I have an application that uses CMP with stateless Entity Beans to access a MySQL database, and I need some information on what I can/should do to protect against colliding database operations. The important points are:

1. This application is the only one that is allowed to access this particular database, but

2. The application is heavily multi-threaded.

Although no two threads *should* be trying to update the same record at the same time, we all know that weird things happen sometimes. So my question is this: how do I perform a serializable "upsert" (update if present, insert otherwise) in this scenario?

Many thanks,
Shankar Tanikella
Ranch Hand

Joined: Jan 30, 2011
Posts: 329

Hi Lyn,
If I get the scenario you described correctly, I prefer programmatic control over this i.e. I shall verify if the record is available for my unique key in the DB and do an update operation if already existing or else create one - all this at my data access layer. However, if you need some rules to manage your concurrency, links here and here would/should help you do that. But personally i do not like using so called "locks" until and unless it is really necessary.

Have Fun with Java
little,little.. little by little makes a lot..
I agree. Here's the link:
subject: "Upsert" using Container Managed Persistence?
It's not a secret anymore!