hibernate: multiple datasources, one transaction

P

pete leg

hi all,
i need to update/modify more than 1 db (on different machines and with
different dbms) so that if one of these actions doesn't work i can roll
back on all the databases involved.

any idea about how to achieve this goal?
thanx in advance
pete
 
B

Babu Kalakrishnan

pete said:
i need to update/modify more than 1 db (on different machines and with
different dbms) so that if one of these actions doesn't work i can roll
back on all the databases involved.

any idea about how to achieve this goal?

Don't think there's anything off the shelf that can do this. As far as I
can recall, you need two separate SessionFactories to handle two
datasources, so you will never have a single Hibernate session that can
handle accesses to the two datasources.

What you could do is to perhaps write a helper class that implements the
Transaction interface and handles two (or more) sessions (i.e internally
creates separate transactions from the 2 sessions, and performs a commit
or rollback on them simultaneously - however, I don't think even this
will solve the problem of a commit on the second database throwing an
exception - because the first commit would have already been performed
by then and you wouldn't be able to roll it back.)

BK
 
R

Robert Klemme

Don't think there's anything off the shelf that can do this. As far as I
can recall, you need two separate SessionFactories to handle two
datasources, so you will never have a single Hibernate session that can
handle accesses to the two datasources.

The concept is called "distributed transaction". As far as I can see
Hibernate itself does not deal with that but it can be included in a CMP
scenario in J2EE:

http://www.hibernate.org/hib_docs/v3/reference/en/html/session-configuration.html#configuration-j2ee
What you could do is to perhaps write a helper class that implements the
Transaction interface and handles two (or more) sessions (i.e internally
creates separate transactions from the 2 sessions, and performs a commit
or rollback on them simultaneously - however, I don't think even this
will solve the problem of a commit on the second database throwing an
exception - because the first commit would have already been performed
by then and you wouldn't be able to roll it back.)

If hibernate is actually capable of participating in distributed TX that
likely means it is capable of doing two phase commit. If it does, then
it should be possible to use that for doing transactions across several
hibernate datastores. You might have to use an external TX coordinator
- or create one yourself.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top