Solved

Java transaction API : to COMMIT part of transaction

Posted on 2010-08-17
7
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 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…

724 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