PDA

View Full Version : Saving to database and properties file in same transaction



Sangy
Jun 3rd, 2009, 06:56 AM
I am using JPA(Hibernate implementation) to store entities in database .

I have a scenario wherein i need to save values to database and also some part of it to
a properties file(maintain preferences for some component). I am using commons configuration
for this .

I want that the transaction consists of both the saves to database and properties file as well .
There is a method in PropertiesConfiguration(commons config object) , save , what i want is
Spring to either call commit on connection and save on PropertiesConfiguration both or none .

How can this be achieved ?

Thanks in advance .

chudak
Jun 3rd, 2009, 10:27 AM
You can't achieve true transactional semantics with the configuration that you've described because the properties configuration does not support ACID.

You'd need to look into using something like this:

http://www.jboss.org/jbosstm/fileio/

Mark Fisher
Jun 3rd, 2009, 11:14 AM
An option worth considering is to use Spring's TransactionSynchronizationManager. You can register a TransactionSynchronization and add the save() method call within one of its methods (e.g. afterCompletion and check the status for COMMITED vs. ROLLED_BACK). It is not true 2PC, but it does allow you to have conditional invocation of a non-transactional resource based on the outcome of the transactional resource's operation.

nitin_verma
Feb 11th, 2012, 12:05 PM
Hi,

Using XA transactions you can combine operations across multiple resources in a single global transaction. This would guarantee that either all of the operations commit or none of them commit. You need to configure your database's data-source to enable XA with database. As file-systems do not support XA, you can use XADisk (http://xadisk.java.net/) to perform the file operations inside XA transactions.

Hope that helps.

Thanks,
Nitin