Solved

Java transaction API : to COMMIT part of transaction

Posted on 2010-08-17
7
351 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

803 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