Solved

Java transaction API : to COMMIT part of transaction

Posted on 2010-08-17
7
352 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 27

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 53
servlet  URL Rewriting 1 42
Apps blocked by Java 9 88
login jsp example 24 68
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

830 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