Solved

transactions in  jsp

Posted on 2003-11-12
13
483 Views
Last Modified: 2012-05-04
I have a jsp page which contains a number of sql statements say 2 inserts,
1 delete and 1 update. They are a part of one transaction. So I want either none is executed or all of them r executed. How do I make sure that this happens. Do i need to do something in the jsp code to ensure or will my database(mysql) handle this.
pz suggest
0
Comment
Question by:will_789
[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
  • 4
  • 2
13 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 170 total points
ID: 9730598
<%
   connection.setAutoCommit( false ) ;
   try
   {
     // do an update

     // do an insert
 
     // do a delete

     connection.commit() ;
   }
   catch( Exception ex )
   {
      ex.printStackTrace() ;
      connection.rollback() ;
   }
%>

Assuming your database allows commit and rollback :-)
0
 
LVL 1

Assisted Solution

by:JNic
JNic earned 165 total points
ID: 9730605
You should use InnoDB tables which is supported by mySQL from version 3.23.34a.

Read more about them here: http://www.mysql.com/doc/en/InnoDB.html

Then you will be able to do things like the following:

// assuming you have a connection
 connection.setAutoCommit(false);
 connection.setTransactionIsolation(4);
 statement = connection.createStatement();
  for (int i=0; i<10; i++){
      String update="Insert into numbers (number) values (i)";
      statement.executeUpdate(update);
   }
   boolean doIt=false;
   // Then do some testing to see if you want the changes or not.
  if (doIt){
      statement.commit(); // this executes all the updates
  }
  else{
     statement.rollback(); // this eleminates all the updates
  }
}

If my memory serves me right, a SQLException will do a rollback automatically. - Other Exceptions will not.

Hope it helped!

Regards,

Nic


 

0
 
LVL 1

Expert Comment

by:JNic
ID: 9730612
Oh - I'm too slow ;-)
0
Independent Software Vendors: 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 35

Expert Comment

by:TimYates
ID: 9730635
Hehehe ;-)

Not sure if he's using MySQL either...

Databases like Postgres or Oracle can rollback anyway (no special table type needed)

And you have to watch out if you are using something like MS Access, as turning auto-commit off will have a HUGE performance hit

(as it will lock an entire db page till you commit or rollback, not a single row as with the others)

Tim.
0
 
LVL 1

Expert Comment

by:JNic
ID: 9730658
Quote from TimYates: Not sure if he's using MySQL either...
Quote from question: "my database(mysql)"

MUAHAHAHAHAHAHAHAAAAA!!!!
;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9730669
Whoops! >_<

hehehehehehehe ;-)
0
 

Author Comment

by:will_789
ID: 9730673
Could u plz tell me what's the purpose of

connection.setTransactionIsolation(4);
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9730705
I think the 4 is:

Connection.TRANSACTION_SERIALIZABLE

Which means:

--------

A constant indicating that dirty reads, non-repeatable reads and phantom reads are prevented. This level includes the prohibitions in TRANSACTION_REPEATABLE_READ and further prohibits the situation where one transaction reads all rows that satisfy a WHERE condition, a second transaction inserts a row that satisfies that WHERE condition, and the first transaction rereads for the same condition, retrieving the additional "phantom" row in the second read.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9730718
Whoops!

Connection.TRANSACTION_REPEATABLE_READ == 4 ;

A constant indicating that dirty reads and non-repeatable reads are prevented; phantom reads can occur. This level prohibits a transaction from reading a row with uncommitted changes in it, and it also prohibits the situation where one transaction reads a row, a second transaction alters the row, and the first transaction rereads the row, getting different values the second time (a "non-repeatable read").
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9730728
it means that other people reading the database will not see different data until the transaction has been committed
0
 
LVL 1

Expert Comment

by:JNic
ID: 9730744
Oops I actually did not mean to write that line:

connection.setTransactionIsolation(4);

like that.

This method has four different settings:

connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
connection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);

Read more about what the settings mean here:

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Connection.html

Look under FIELD DETAIL
0
 

Author Comment

by:will_789
ID: 9730752
thanks a lot
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9730763

TRANSACTION_READ_COMMITTED

or

TRANSACTION_REPEATABLE_READ

Are usually the best ones to use...  It's a matter of personal choice, and whether you want repeatable reads...
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This article is in regards to the Cisco QSFP-4SFP10G-CU1M cables, which are designed to uplink/downlink 40GB ports to 10GB SFP ports. I recently experienced this and found very little configuration documentation on how these are supposed to be confi…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

627 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