From: <jbo...@li...> - 2006-05-04 09:41:21
|
Author: mar...@jb... Date: 2006-05-04 05:41:00 -0400 (Thu, 04 May 2006) New Revision: 4083 Modified: labs/jbosstm/trunk/ArjunaJTS/trailmap/README.txt labs/jbosstm/trunk/ArjunaJTS/trailmap/src/com/arjuna/demo/jta/jdbcbank/Bank.java Log: Fixed trailmap. http://jira.jboss.com/jira/browse/JBTM-58 Modified: labs/jbosstm/trunk/ArjunaJTS/trailmap/README.txt =================================================================== --- labs/jbosstm/trunk/ArjunaJTS/trailmap/README.txt 2006-05-04 09:40:18 UTC (rev 4082) +++ labs/jbosstm/trunk/ArjunaJTS/trailmap/README.txt 2006-05-04 09:41:00 UTC (rev 4083) @@ -1,53 +1,84 @@ -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. -The trail map is provided with examples (the Banking application) that -allow a better understanding of the way to use the JBossTS -Programming interfaces. - -To build the sources files you should follow instructins given below: - - -- Ensure you have the Ant build system installed. Ant is a Java build -tool, similar to make. - It is available for free from http://ant.apache.org/ - The sample application requires version 1.5.1 or later. - -- The PATH and CLASSPATH environment variables need to be set -appropriately to use Arjuna Transaction Service. - - To make this easier, we provide a shell script setup-env.sh (and for - Windows a batch file setup-env.bat) which you can either source, or - use to input into your own environment. These scripts are located in - in the directory <arjunats_install_root>/bin/ - -Important Note: - - Ensure that, in your CLASSPATH, any JBossTS jar file appears before - the jacorb (version 2.2.2) jar files - - From a command prompt, go (or 'cd') to the directory containing the - build.xml file (<arjunats_install_root>/trailmap) and type 'ant'. - - Add the generated file named arjunats-demo.jar and located under - <arjunats_install_root>/trailmap/lib in you CLASSPATH environment - variable. - - For each sample, refer to the appropriate trail page. - - +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. +The trail map is provided with examples (the Banking application) that +allow a better understanding of the way to use the JBossTS +Programming interfaces. + +To build the sources files you should follow instructins given below: + + +- Ensure you have the Ant build system installed. Ant is a Java build +tool, similar to make. + It is available for free from http://ant.apache.org/ + The sample application requires version 1.5.1 or later. + +- The PATH and CLASSPATH environment variables need to be set +appropriately to use Arjuna Transaction Service. + + To make this easier, we provide a shell script setup-env.sh (and for + Windows a batch file setup-env.bat) which you can either source, or + use to input into your own environment. These scripts are located in + in the directory <arjunats_install_root>/bin/ + + JNDI is recommended way to use XADataSource because it isolates the application from the + different jdbc implementations. The JNDI implementation, that the jdbcbank sample uses is fscontext.jar, + which can be download from http://java.sun.com/products/jndi/downloads/index.html + +Important Note: + + Ensure that, in your CLASSPATH, any JBossTS jar file appears before + the jacorb (version 2.2.2) jar files + + From a command prompt, go (or 'cd') to the directory containing the + build.xml file (<arjunats_install_root>/trailmap) and type 'ant'. + + Add the generated file named arjunats-demo.jar and located under + <arjunats_install_root>/trailmap/lib in you CLASSPATH environment + variable. + + For each sample, refer to the appropriate trail page. + + Database Note: + + The out-of-the-box configuration assumes an Oracle database. If you want + to use MSSQLServer, then you need to do the following: + + 1. install SQL Server JDBC XA procedures http://edocs.bea.com/wls/docs81/jdbc_drivers/mssqlserver.html#1075232 + 2. Start up the Microsoft Distributed Transaction Coordinator (DTC) http://msdn2.microsoft.com/en-US/library/ms378931(SQL.90).aspx + +If to shift from using Oracle as in jdbcbank example to Microsoft SQLServer 2000, the +initialization code for XADataSource should be replaced. The jdbc driver which may be used is available from: +http://www.microsoft.com/downloads/details.aspx?familyid=07287B11-0502-461A-B138-2AA54BFDC03A&displaylang=en + +<code> + SQLServerDataSource ds = new com.microsoft.jdbcx.sqlserver.SQLServerDataSource(); + ds.setDescription("MSSQLServer2k DataSource"); + ds.setServerName(host); + ds.setPortNumber(1433); + ds.setDatabaseName(dbName); + ds.setSelectMethod("cursor"); //It's a Must emphasized in Driver's User Manual +</code> +to replace +<code> + Class oracleXADataSource = Class.forName("oracle.jdbc.xa.client.OracleXADataSource"); + DataSource ds = (DataSource) oracleXADataSource.newInstance(); + Method setUrlMethod = oracleXADataSource.getMethod("setURL", new Class[]{String.class}); + setUrlMethod.invoke(ds, new Object[]{new String("jdbc:oracle:thin:@" + host + ":" + port + ":" + dbName)}); +</code> + Modified: labs/jbosstm/trunk/ArjunaJTS/trailmap/src/com/arjuna/demo/jta/jdbcbank/Bank.java =================================================================== --- labs/jbosstm/trunk/ArjunaJTS/trailmap/src/com/arjuna/demo/jta/jdbcbank/Bank.java 2006-05-04 09:40:18 UTC (rev 4082) +++ labs/jbosstm/trunk/ArjunaJTS/trailmap/src/com/arjuna/demo/jta/jdbcbank/Bank.java 2006-05-04 09:41:00 UTC (rev 4083) @@ -1,276 +1,284 @@ -/* - * 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. - */ -/* - * Copyright (C) 2003, 2004 - * Arjuna Technologies Limited - * Newcastle upon Tyne, UK - * - * $Id: Bank.java 2342 2006-03-30 13:06:17Z $ - */ - -package com.arjuna.demo.jta.jdbcbank; - -import com.arjuna.ats.jdbc.TransactionalDriver; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Properties; - - -/** - * This is the JDBC implementation of the Bank class used in the Arjuna Transactions product trailmap. It is used to - * create and clean the database that the example uses as well as perform the banking operations that all Bank - * implementations in the trailmap provide, such as creating and accessing an account. - */ -public class Bank -{ - /** - * An implementation of an SQL driver which can be used for transactional access to a database. - */ - private TransactionalDriver arjunaJDBC2Driver; - - /** - * The properties are used by the connection to log in to the database. - */ - private static Properties dbProperties; - - /** - * Creates a new JDBC-backed bank object. - */ - public Bank() - { - try - { - // Register the driver to use - DriverManager.registerDriver(new TransactionalDriver()); - - // Populate the connections properties with the required security credentials - dbProperties = new Properties(); - dbProperties.put(TransactionalDriver.userName, BankClient.user); - dbProperties.put(TransactionalDriver.password, BankClient.password); - - // Create the transactional driver to use to create the database - arjunaJDBC2Driver = new TransactionalDriver(); - - // Create the table (will drop an existing table if it already exists) - create_table(); - } - catch (Exception e) - { - e.printStackTrace(); - System.exit(0); - } - } - - /** - * Create a new bank account. This will reserve space in the database for the account and it's initial amount - * of cash. - * - * @param accountName The name of the account to open. - * @param openingBalance The opening balance of the account. - */ - public void create_account(String accountName, float openingBalance) - { - try - { - // Obtain a connection to the database using the transactional driver - Connection connection = arjunaJDBC2Driver.connect("jdbc:arjuna:jdbc/DB", dbProperties); - - // Create a statement and execute an update to the database to reserve space for the new bank account - Statement stmtx = connection.createStatement(); // tx statement - stmtx.executeUpdate("INSERT INTO accounts (accountName, value) VALUES ('" + accountName + "'," + openingBalance + ")"); - } - catch (SQLException e) - { - // We should set the rollback_only - System.out.println("SQL Exception ..........."); - e.printStackTrace(); - } - } - - /** - * Get the current balance of a required bank account. This will look at the database record for the account - * specified. - * - * @param accountName The name of the account to view. - * @return The balance of the requested account. - * - * @throws NotExistingAccount If the account does not exist. - */ - public float get_balance(String accountName) throws NotExistingAccount - { - float theBalance = 0; - try - { - // Obtain a connection to the database using the transactional driver - Connection connection = arjunaJDBC2Driver.connect("jdbc:arjuna:jdbc/DB", dbProperties); - - // Create a statement and execute a query on the database to determine the balance of the account - Statement stmtx = connection.createStatement(); // tx statement - ResultSet rs = stmtx.executeQuery("SELECT value from accounts WHERE name = '" + accountName + "'"); - - // Keep iterating through the result set, though there should only be one value - while (rs.next()) - { - theBalance = rs.getFloat("value"); - } - } - - catch (SQLException e) - { - // We should set the rollback_only - System.out.println("SQL Exception ..........."); - e.printStackTrace(); - throw new NotExistingAccount("The Account requested does not exist"); - } - - return theBalance; - } - - /** - * Debit the specified account by the required value. This will transactionally remove funds from the account by - * executing an update on the underlying JDBC data. - * - * @param accountName The name of the account to debit from. - * @param debitValue The amount of cash to debit from the account. - * - * @throws NotExistingAccount If the account does not exist. - */ - public void debit(String accountName, float debitValue) throws NotExistingAccount - { - try - { - // Obtain a connection to the database using the transactional driver - Connection connection = arjunaJDBC2Driver.connect("jdbc:arjuna:jdbc/DB", dbProperties); - - // Create a statement and execute an update to the database to debit the required account - Statement stmtx = connection.createStatement(); // tx statement - stmtx.executeUpdate("UPDATE accounts Set value = value - " + debitValue + " WHERE name = '" + accountName + "'"); - } - catch (SQLException e) - { - // We should set the rollback_only - System.out.println("SQL Exception ..........."); - e.printStackTrace(); - throw new NotExistingAccount("The Account requested does not exist"); - } - } - - /** - * Credit a specified bank account with the required amount. This method will transactionally update the underlying - * datastore with the amount of cash to add. - * - * @param accountName The name of the account to credit. - * @param creditAmount The amount of cash to credit the account with. - * - * @throws NotExistingAccount If the account does not exist. - */ - public void credit(String accountName, float creditAmount) throws NotExistingAccount - { - try - { - // Obtain a connection to the database using the transactional driver - Connection conne = arjunaJDBC2Driver.connect("jdbc:arjuna:jdbc/DB", dbProperties); - - // Create a statement and execute an update to the database to credit the required account - Statement stmtx = conne.createStatement(); // tx statement - stmtx.executeUpdate("UPDATE accounts Set value = value + " + creditAmount + " WHERE name = '" + accountName + "'"); - } - catch (SQLException e) - { - //We should set the rollback_only - System.out.println("SQL Exception ..........."); - e.printStackTrace(); - throw new NotExistingAccount("The Account requested does not exist"); - } - } - - /** - * This will allow the bank to transactionally move money from one account to another. - * - * @param fromAccount The account to remove money from. - * @param toAccount The account to add money to. - * @param transferAmount The amount of money to transfer. - * - * @throws NotExistingAccount If the account does not exist already. - */ - public void transfer(String fromAccount, String toAccount, float transferAmount) throws NotExistingAccount - { - try - { - // Obtain a connection to the database using the transactional driver - Connection connection = arjunaJDBC2Driver.connect("jdbc:arjuna:jdbc/DB", dbProperties); - - // Create a statement and execute an update to the database to credit the required account and debit the - // other account - Statement stmtx = connection.createStatement(); // tx statement - stmtx.executeUpdate("UPDATE accounts Set value = value - " + transferAmount + " WHERE name = '" + fromAccount + "'"); - stmtx.executeUpdate("UPDATE accounts Set value = value + " + transferAmount + " WHERE name = '" + toAccount + "'"); - } - catch (SQLException e) - { - //We should set the rollback_only - System.out.println("SQL Exception ..........."); - e.printStackTrace(); - throw new NotExistingAccount("The Account requested does not exist"); - } - } - - /** - * This utility method will create the bank account table if it does not already exist. If it does exist and the - * client has requested that the table be cleaned then the table is first dropped. - */ - private void create_table() - { - try - { - // Obtain a connection to the database using the transactional driver - System.out.println("\nCreating connection to database: "); - - // A JDBC connection is used to create and drop (where neccessary) the underlying database table - Connection connection = arjunaJDBC2Driver.connect("jdbc:arjuna:jdbc/DB", dbProperties); - - Statement stmt = connection.createStatement(); // non-tx statement - if (BankClient.clean) - { - System.out.println("\nDrop the table "); - stmt.executeUpdate("DROP TABLE accounts"); - } - - if (BankClient.clean || BankClient.create) - stmt.executeUpdate("CREATE TABLE accounts (name VARCHAR(10) NOT NULL UNIQUE, value REAL)"); - - stmt.close(); - connection.close(); - } - catch (SQLException e) - { - e.printStackTrace(); - System.exit(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. + */ +/* + * Copyright (C) 2003, 2004 + * Arjuna Technologies Limited + * Newcastle upon Tyne, UK + * + * $Id: Bank.java 2342 2006-03-30 13:06:17Z tjenkinson $ + */ + +package com.arjuna.demo.jta.jdbcbank; + +import com.arjuna.ats.jdbc.TransactionalDriver; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; + + +/** + * This is the JDBC implementation of the Bank class used in the Arjuna Transactions product trailmap. It is used to + * create and clean the database that the example uses as well as perform the banking operations that all Bank + * implementations in the trailmap provide, such as creating and accessing an account. + */ +public class Bank +{ + /** + * An implementation of an SQL driver which can be used for transactional access to a database. + */ + private TransactionalDriver arjunaJDBC2Driver; + + /** + * The properties are used by the connection to log in to the database. + */ + private static Properties dbProperties; + + /** + * Creates a new JDBC-backed bank object. + */ + public Bank() + { + try + { + // Register the driver to use + DriverManager.registerDriver(new TransactionalDriver()); + + // Populate the connections properties with the required security credentials + dbProperties = new Properties(); + dbProperties.put(TransactionalDriver.userName, BankClient.user); + dbProperties.put(TransactionalDriver.password, BankClient.password); + + // Create the transactional driver to use to create the database + arjunaJDBC2Driver = new TransactionalDriver(); + + // Create the table (will drop an existing table if it already exists) + create_table(); + } + catch (Exception e) + { + e.printStackTrace(); + System.exit(0); + } + } + + /** + * Create a new bank account. This will reserve space in the database for the account and it's initial amount + * of cash. + * + * @param accountName The name of the account to open. + * @param openingBalance The opening balance of the account. + */ + public void create_account(String accountName, float openingBalance) + { + try + { + // Obtain a connection to the database using the transactional driver + Connection connection = arjunaJDBC2Driver.connect("jdbc:arjuna:jdbc/DB", dbProperties); + + // Create a statement and execute an update to the database to reserve space for the new bank account + Statement stmtx = connection.createStatement(); // tx statement + stmtx.executeUpdate("INSERT INTO accounts (name, value) VALUES ('" + accountName + "'," + openingBalance + ")"); + } + catch (SQLException e) + { + // We should set the rollback_only + System.out.println("SQL Exception ..........."); + e.printStackTrace(); + } + } + + /** + * Get the current balance of a required bank account. This will look at the database record for the account + * specified. + * + * @param accountName The name of the account to view. + * @return The balance of the requested account. + * + * @throws NotExistingAccount If the account does not exist. + */ + public float get_balance(String accountName) throws NotExistingAccount + { + float theBalance = 0; + try + { + // Obtain a connection to the database using the transactional driver + Connection connection = arjunaJDBC2Driver.connect("jdbc:arjuna:jdbc/DB", dbProperties); + + // Create a statement and execute a query on the database to determine the balance of the account + Statement stmtx = connection.createStatement(); // tx statement + ResultSet rs = stmtx.executeQuery("SELECT value from accounts WHERE name = '" + accountName + "'"); + + // Keep iterating through the result set, though there should only be one value + while (rs.next()) + { + theBalance = rs.getFloat("value"); + } + } + + catch (SQLException e) + { + // We should set the rollback_only + System.out.println("SQL Exception ..........."); + e.printStackTrace(); + throw new NotExistingAccount("The Account requested does not exist"); + } + + return theBalance; + } + + /** + * Debit the specified account by the required value. This will transactionally remove funds from the account by + * executing an update on the underlying JDBC data. + * + * @param accountName The name of the account to debit from. + * @param debitValue The amount of cash to debit from the account. + * + * @throws NotExistingAccount If the account does not exist. + */ + public void debit(String accountName, float debitValue) throws NotExistingAccount + { + try + { + // Obtain a connection to the database using the transactional driver + Connection connection = arjunaJDBC2Driver.connect("jdbc:arjuna:jdbc/DB", dbProperties); + + // Create a statement and execute an update to the database to debit the required account + Statement stmtx = connection.createStatement(); // tx statement + stmtx.executeUpdate("UPDATE accounts Set value = value - " + debitValue + " WHERE name = '" + accountName + "'"); + } + catch (SQLException e) + { + // We should set the rollback_only + System.out.println("SQL Exception ..........."); + e.printStackTrace(); + throw new NotExistingAccount("The Account requested does not exist"); + } + } + + /** + * Credit a specified bank account with the required amount. This method will transactionally update the underlying + * datastore with the amount of cash to add. + * + * @param accountName The name of the account to credit. + * @param creditAmount The amount of cash to credit the account with. + * + * @throws NotExistingAccount If the account does not exist. + */ + public void credit(String accountName, float creditAmount) throws NotExistingAccount + { + try + { + // Obtain a connection to the database using the transactional driver + Connection conne = arjunaJDBC2Driver.connect("jdbc:arjuna:jdbc/DB", dbProperties); + + // Create a statement and execute an update to the database to credit the required account + Statement stmtx = conne.createStatement(); // tx statement + stmtx.executeUpdate("UPDATE accounts Set value = value + " + creditAmount + " WHERE name = '" + accountName + "'"); + } + catch (SQLException e) + { + //We should set the rollback_only + System.out.println("SQL Exception ..........."); + e.printStackTrace(); + throw new NotExistingAccount("The Account requested does not exist"); + } + } + + /** + * This will allow the bank to transactionally move money from one account to another. + * + * @param fromAccount The account to remove money from. + * @param toAccount The account to add money to. + * @param transferAmount The amount of money to transfer. + * + * @throws NotExistingAccount If the account does not exist already. + */ + public void transfer(String fromAccount, String toAccount, float transferAmount) throws NotExistingAccount + { + try + { + // Obtain a connection to the database using the transactional driver + Connection connection = arjunaJDBC2Driver.connect("jdbc:arjuna:jdbc/DB", dbProperties); + + // Create a statement and execute an update to the database to credit the required account and debit the + // other account + Statement stmtx = connection.createStatement(); // tx statement + stmtx.executeUpdate("UPDATE accounts Set value = value - " + transferAmount + " WHERE name = '" + fromAccount + "'"); + stmtx.executeUpdate("UPDATE accounts Set value = value + " + transferAmount + " WHERE name = '" + toAccount + "'"); + } + catch (SQLException e) + { + //We should set the rollback_only + System.out.println("SQL Exception ..........."); + e.printStackTrace(); + throw new NotExistingAccount("The Account requested does not exist"); + } + } + + /** + * This utility method will create the bank account table if it does not already exist. If it does exist and the + * client has requested that the table be cleaned then the table is first dropped. + */ + private void create_table() + { + try + { + // Obtain a connection to the database using the transactional driver + System.out.println("\nCreating connection to database: "); + + // A JDBC connection is used to create and drop (where neccessary) the underlying database table + Connection connection = arjunaJDBC2Driver.connect("jdbc:arjuna:jdbc/DB", dbProperties); + + Statement stmt = connection.createStatement(); // non-tx statement + if (BankClient.clean) + { + System.out.println("\nDrop the table "); + stmt.executeUpdate("DROP TABLE accounts"); + } + + if (BankClient.clean || BankClient.create) + { + stmt.executeUpdate("CREATE TABLE accounts (name VARCHAR(10) NOT NULL UNIQUE, value REAL)"); + + if (BankClient.clean) + BankClient.clean = false; + + if (BankClient.create) + BankClient.create = false; + } + + stmt.close(); + connection.close(); + } + catch (SQLException e) + { + e.printStackTrace(); + System.exit(0); + } + } +} + + + |