<?xml version="1.0"?>
<!--
  JBoss, Home of Professional Open Source
  Copyright 2006, JBoss Inc., and individual contributors as indicated
  by the @authors tag.  All rights reserved. 
  See the copyright.txt in the distribution for a full listing 
  of individual contributors.
  This copyrighted material is made available to anyone wishing to use,
  modify, copy, or redistribute it subject to the terms and conditions
  of the GNU General Public License, v. 2.0.
  This program is distributed in the hope that it will be useful, but WITHOUT A 
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
  PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  You should have received a copy of the GNU General Public License,
  v. 2.0 along with this distribution; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
  MA  02110-1301, USA.
  
  (C) 2005-2006,
  @author JBoss Inc.
-->
<page title="Arjuna Technologies Limited: Trail map: Managing Transaction in J2EE" rootpath="../..">
	<header>
        Trail map: JDBC and Transactions
    </header>
	<content>
		<navigation>
			<links include="includes/www_arjuna_com_navigation.xml"/>
			<links include="includes/trailmap_navigation.xml"/>
			<links title="Navigation">
				<link ref="index.html">Home</link>
				<link ref="JTA_Overview.html">Previous</link>
			</links>
			<links include="includes/trailmap_additional.xml"/>
		</navigation>
		<area title="JDBC and Transactions">
			<html>
				<p> Java Data Base Connectivity, provide Java programs with a way to connect to 
  and use relational databases. The JDBC API lets you invoke SQL commands from 
  Java programming language methods. In simplest terms, JDBC allows to do three 
  things</p>
<ol>
	<li>Establish a connection with a database </li>
	<li>Send SQL statements </li>
	<li>Process the results </li>
</ol>
<p>The following code fragment gives a simple example of these three steps:</p>
<pre>
Connection con = DriverManager.getConnection(
  &quot;jdbc:myDriver:wombat&quot;, &quot;myLogin&quot;, &quot;myPassword&quot;);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(&quot;SELECT a, b, c FROM Table1&quot;);
while (rs.next()) {
  int x = rs.getInt(&quot;a&quot;);
  String s = rs.getString(&quot;b&quot;);
  float f = rs.getFloat(&quot;c&quot;); 
}
</pre>

<p>Before the version 2.0 of JDBC, only local transactions controlled by the transaction 
  manager of the DBMS is possible. To code a JDBC transaction, you invoke the 
  commit and rollback methods of the java.sql.Connection interface. The beginning 
  of a transaction is implicit. A transaction begins with the first SQL statement 
  that follows the most recent commit, rollback, or connect statement. (This rule 
  is generally true, but may vary with DBMS vendor.). The following example illustrates 
  how transactions are managed by the JDBC API.</p>
<pre>
public void withdraw (double amount) {
  try {
    //A connection opened with JDBC is an AUTO COMMIT mode meaning 
    // that the commitment is automatically performed when the connection 
    // is closed
    //setAutoCommit to false disable this feature
    connection.setAutoCommit(false);
    //perform an SQL update to Withdraw money from account
    connection.commit();
  } catch (Exception ex) {
      try {
         connection.rollback();
          throw new Exception(&quot;Transaction failed: &quot; +  ex.getMessage());
      } catch (Exception sqx) {
           throw new Exception(...}
      }
  }
}</pre>

<p>From the version 2.0, a JDBC driver can be involved within a distributed transaction 
  since it supports the XAResource interface that allows to participate to the 
  2PC protocol. An application that need to include more than one database can 
  create a JTA transaction. To demarcate a JTA transaction, the application program 
  invokes the begin, commit, and rollback methods of the javax.transaction.UserTransaction 
  interface. The following code, that can be applied to a bean-managed transaction, 
  demonstrates the UserTransaction methods. The begin and commit invocations delimit 
  the updates to the database. If the updates fail, the code invokes the rollback 
  method and throws an Exception. </p>
<pre>
public void transfer(double amount) {
  UserTransaction ut = context.getUserTransaction();
  
  try {
     ut.begin();
     // Perform SQL command to debit account 1
     // Perform SQL command to debit account 2
     ut.commit();
   } catch (Exception ex) {
        try {
          ut.rollback();
        } catch (Exception ex1) {
             throw new Exception (&quot;Rollback failed: &quot; + ex1.getMessage());
        }
        throw new Exception (&quot;Transaction failed: &quot; + ex.getMessage());
   }
}
</pre>
			</html>
		</area>
	</content>
	<footer/>
</page>
