Solved

Java transaction API : to COMMIT part of transaction

Posted on 2010-08-17
7
345 Views
Last Modified: 2012-05-10
hi guys
I am working on spring and hibernate project. In DAO layer i am using Java transaction API. JTA :

Javax.Transaction.UserTransaction api

userTransaction t = ic.lookup(java:comp/UserTransaction)
t.begin()
session.load(); //generates sql to retrieve row from databse
sessio.saveorupdate(); //generates sql to save object in database
..
t.commit().

My question is if there is a problem with the above transaction say for example
session.load();  is sucessful but session.saveorupdate(); fails,
the entire transaction would roll back, so in that case session.load(); would also fail (because the entire transaction is rolled back.)

My requirment is to make the sucessful part of the transaction to commit. so in this case i want
session.load();  to commit although sessio.saveorupdate(); is rolled back.

Any idea how i can modify my above code.

thanks.
0
Comment
Question by:royjayd
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 33457337
>>so in this case i want session.load();  to commit although sessio.saveorupdate(); is rolled back.

I don't know what session.load does, but it doesn't sound like a method that has much to do with commitable statements. If i'm wrong, please post the code of that method
0
 

Author Comment

by:royjayd
ID: 33457879
>>I don't know what session.load does
ok ..
a simpler example would be

userTransaction t = ic.lookup(java:comp/UserTransaction)

t.begin()
java.sql.Connection  conn = DriverManager.getConnection();
PrepareStatement select = conn.prepareStatement("select * from Customer;");  //retrieves rows from table.
select.executeUpdate();
PrepareStatement insert =conn.prepareStatement("Insert into Employee values") // inserts values into table
insert.executeUpdate();
..
t.commit().

if select.executeUpdate();  is sucessful and if  insert.executeUpdate(); fails ,  instead of rolling back the entire transaction i would like to commit select.executeUpdate();
0
 
LVL 10

Assisted Solution

by:Hegemon
Hegemon earned 250 total points
ID: 33458286
Your scenario makes little sense.
You don't need to commit a transaction that has not made any changes. session.load() does exactly this: loads the data using a SELECT statement but does not make any changes.

select.executeUpdate() is also wrong - you don't have any updates.

Thus the whole problem looks incorrect: if your 2nd statement fails, there is nothing in the 1st statement that can be committed.

If you had anything to commit after the 1st statement, you would probably want to create a nested transaction before starting the 2nd.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 33460004
you need to use multiple tx's. you can't commit/rollback part of a tx
0
 

Author Comment

by:royjayd
ID: 33460856
any sample code of multiple tx's or nested tx's would greatly help

thanks.
0
 
LVL 26

Expert Comment

by:mrcoffee365
ID: 37105855
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now