Section 12.3. Transactions

   

12.3 Transactions

You may recall from ColdFusion working with the < cftransaction > tag, which allows you to create a logical group of statements to be sent to a database. They are meant to all be executed, and if one or more cannot be executed, any executed statements must be undone. That is, having specified a series of SQL statements as a transaction group, the entire group can be rolled back if necessary. They also allow you to undo an operation if an exception occurred during a database query. Transaction blocks are defined like this in ColdFusion:

 <cftry>  <cftransaction action = "begin">     <cfquery name = q" datasource = "#request.dsn#">           INSERT INTO Products (productName, price)           VALUES ('#form.productName#', 75);      </cfquery> <cfcatch type="DATABASE">    <!--- roll back the insertion if an exception occurred--->    <cftransaction action="rollback"> ... </cfcatch> etc.... 

The way to create a transaction in Java's JDBC is to use the Connection object. Once the driver is started, the Connection object by default will automatically commit every change after execution of a statement. The way to turn this off when using commits or rollbacks is to invoke this method of the Connection object:

 // save this value  boolean saveCommitValue = con.getAutoCommit();      // turn automatic committing off con.setAutoComit(false); 

The way to rollback to a prior state in the event of an exception is just as you might expect:

 // undo the partial update  con.rollback(); 

What may appear interesting to a ColdFusion developer here is that the rollback() method is tied to the Connection object.

Note

The fact that everything that happens in Java must be tied to an object can be hard to get your mind around as a CF developer. It's okay ”in fact, it will make your ColdFusion development much better. Really. There is a difficulty associated with having everything tied to objects, and that is that you must know what objects take what methods. The only solution to this stumbling block is to read the API. When you create objects in a Java IDE, type a dot after the object name and pause for a moment ”the IDE should bring up a list of available methods for your current object. This is also a good way to get more familiar with the language. Just remember that in ColdFusion, the processing is done by a Web browser, and as such, is top to bottom. In Java (even in JSPs in an important way), processing is only top to bottom within the main methods. You've written enough main methods to know that this is hardly the point. Most of Java happens when objects create and use other objects in an asynchronous manner. Your main method is generally quite short and non-descript. But things may not appear out of the blue. This is a long-winded way of explaining that you can just write <cftransaction> in the middle of a page before a query in ColdFusion, but you must call a method on an object in Java to do that work. Now, once you start working with the JavaServer Pages Standard Tag Library, you'll be able to implement transactions in your JSPs, just as easily as you can in ColdFusion.


Using JDBC 2.0-compliant drivers, you must rollback or commit at some point. It is an all-or-nothing proposition. In Java using JDBC 3.0 you can define savepoints in your applications. Savepoints work somewhat like labels do; they allow you to specify a database state to which you may later like to return. Say you are writing an application that performs some inserts and then some updates. You may wish to break the application logic out in order to allow the inserts to occur, but not keep the updates. The following snippet demonstrates how a savepoint can be used to handle such an operation.

[View full width]
 
[View full width]
Statement stmt = con.createStatement(); int rowcount = stmt.excuteUpdate("INSERT into Users (username, privilege) VALUES ('Chico', graphics/ccc.gif 1)"); // savepoint allows us to return here and // undo only to this point // that way we don't lose the insert we just did Savepoint sp = con.setSavePoint("sp1"); rowcount = stmt.executeUpdate("DELETE from Users"); // this undoes the delete statement, not the insert con.rollback(sp1) // now it is permanent con.commit;

   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net