Solved

Java transaction API : to COMMIT part of transaction

Posted on 2010-08-17
7
350 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best (free) software to access postgres db (java) 1 33
JAVA part two 5 58
advertisement module in core php 4 148
Windows 10 IE Certificate Issue 10 42
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

862 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

27 Experts available now in Live!

Get 1:1 Help Now