From torque-dev-return-6572-apmail-db-torque-dev-archive=db.apache.org@db.apache.org Mon Apr 10 19:37:14 2006 Return-Path: Delivered-To: apmail-db-torque-dev-archive@www.apache.org Received: (qmail 99220 invoked from network); 10 Apr 2006 19:37:14 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 10 Apr 2006 19:37:14 -0000 Received: (qmail 33380 invoked by uid 500); 10 Apr 2006 19:37:13 -0000 Delivered-To: apmail-db-torque-dev-archive@db.apache.org Received: (qmail 33359 invoked by uid 500); 10 Apr 2006 19:37:13 -0000 Mailing-List: contact torque-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Apache Torque Developers List" Reply-To: "Apache Torque Developers List" Delivered-To: mailing list torque-dev@db.apache.org Received: (qmail 33348 invoked by uid 500); 10 Apr 2006 19:37:12 -0000 Received: (qmail 33345 invoked by uid 99); 10 Apr 2006 19:37:12 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 Apr 2006 12:37:12 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Mon, 10 Apr 2006 12:37:11 -0700 Received: (qmail 99107 invoked by uid 65534); 10 Apr 2006 19:36:51 -0000 Message-ID: <20060410193651.99106.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: svn commit: r393033 - in /db/torque/runtime/trunk/xdocs: navigation.xml reference/connections-transactions.xml reference/index.xml Date: Mon, 10 Apr 2006 19:36:50 -0000 To: torque-commits@db.apache.org From: tfischer@apache.org X-Mailer: svnmailer-1.0.7 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: tfischer Date: Mon Apr 10 12:36:48 2006 New Revision: 393033 URL: http://svn.apache.org/viewcvs?rev=393033&view=rev Log: Added documentation concerning transactions and connections Added: db/torque/runtime/trunk/xdocs/reference/connections-transactions.xml Modified: db/torque/runtime/trunk/xdocs/navigation.xml db/torque/runtime/trunk/xdocs/reference/index.xml Modified: db/torque/runtime/trunk/xdocs/navigation.xml URL: http://svn.apache.org/viewcvs/db/torque/runtime/trunk/xdocs/navigation.xml?rev=393033&r1=393032&r2=393033&view=diff ============================================================================== --- db/torque/runtime/trunk/xdocs/navigation.xml (original) +++ db/torque/runtime/trunk/xdocs/navigation.xml Mon Apr 10 12:36:48 2006 @@ -46,8 +46,9 @@ -ยด + + Added: db/torque/runtime/trunk/xdocs/reference/connections-transactions.xml URL: http://svn.apache.org/viewcvs/db/torque/runtime/trunk/xdocs/reference/connections-transactions.xml?rev=393033&view=auto ============================================================================== --- db/torque/runtime/trunk/xdocs/reference/connections-transactions.xml (added) +++ db/torque/runtime/trunk/xdocs/reference/connections-transactions.xml Mon Apr 10 12:36:48 2006 @@ -0,0 +1,179 @@ + + + + + + Torque Runtime Reference - Reading from the database + Thomas Fischer + + + + +
+

+ To read data from or write data to the database, a connection to the + database is required. To be able to connect to a database, + you need to add the relevant information to Torque's configuration, + see the section about + initialisation and configuration. +

+ +

+ Connection and Transaction handling cannot be treated separately + from each other. Some databases, e.g. oracle, may automatically start + a transaction on an sql command, so you might not be able to + get a database connection without a transaction. Then, transactions live + in database connections, so it is not possible to get a transaction + without a database connection. +

+ +

+ There are two ways of connection and transaction handling: +

    +
  • Let Torque take care of it.
  • +
  • Do it yourself.
  • +
+ Unless you know what you are doing, the first approach is recommended. + If you choose the second approach and write incorrect code, this may lead + to connection leaks and/or blocked connections. You have been warned. +

+
+ +
+ +

+ When you use Torque, you need not bother about connections and + transactions. The only thing you have to do is to configure Torque + correctly, then Torque is able to create a database connection whenever + it needs one. For example, if you call the save() method of some object, + Torque retrieves a connection from its internal connection pool, + uses it to save the connection, and returns it to the pool. +

+ +

+ If the database supports transactions and autocommit is turned off, + one Transaction is used for every method which needs a database + connection. See the source code for details. +

+ +
+ +
+ +

+ If you want to manipulate the database in some way which is not + incorporated into Torque, or want to execute several commands in one + transaction, you need to handle connections and transaction + yourself. This is potentially dangerous, as incorrect handling of + connections and transaction may lead to connection leaks + (resulting in errors because no more connections can be opened to the + database) or blocked connections (because some transaction has been + left open). Typically, these conditions appear if a database operation + fails, because error handling is insufficient. +

+ +

+ The following code is recommended if you need to handle connections + and/or transactions yourself: +

+ + +Connection connection = null; +try +{ + connection = Transaction.begin(); + + // do something with connection, e.g. + // someObject.save(connection); + + Transaction.commit(connection); + connection = null; +} +finally +{ + if (connection != null) + { + Transaction.safeRollback(connection); + } +} + + +

+ This code ensures that if any error occurs within the try block, + an attempt is made to rollback the transaction and return it into the + pool. +

+ +

+ If the database supports transactions and autocomit is turned off, + all database operations are executed in a single transaction. + This has the following effect: For example, you execute two saves in the + try block, and the second save fails. The first save operation will be + rolled back, i.e. the database reverts the changes made by the first save. + If you do not like this behaviour, the safe way is to wrap everything in + its own try ... finally block. + The alternative is to use + Transaction.beginOptional(Torque.getDefaultDB(), false) + instead of Transaction.begin() and make sure your + connection pool returns connections in autocommit mode. This might + not work with CLOB and BLOB data fields, however. +

+ +

+ If you use more than one database, you might want to use + Transaction.begin(String databaseName) instead of + Transaction.begin(). +

+ +

+ The following code is NOT recommended, as some databases + (like Oracle) start transactions automatically, and some pools + (like dbcp, Torque's default pool) do not rollback automatically if + a connection is returned to the pool. So under certain conditions, + you might have leftovers from the last transaction in a "fresh" + connection from the pool, or, even worse, the connection blocks + because the pool attempts to set some transaction property which + needs to be set as first command in a transaction. +

+ + +Connection connection = null; +try +{ + // Bad ! No defined transaction state afterwards ! + connection = Torque.getConnection(); + + // do something with connection, e.g. + // someObject.save(connection); + + Transaction.commit(connection); + connection = null; +} +finally +{ + if (connection != null) + { + // Bad ! No rollback or commit ! + Torque.closeConnection(connection); + } +} + + +
+ + +
\ No newline at end of file Modified: db/torque/runtime/trunk/xdocs/reference/index.xml URL: http://svn.apache.org/viewcvs/db/torque/runtime/trunk/xdocs/reference/index.xml?rev=393033&r1=393032&r2=393033&view=diff ============================================================================== --- db/torque/runtime/trunk/xdocs/reference/index.xml (original) +++ db/torque/runtime/trunk/xdocs/reference/index.xml Mon Apr 10 12:36:48 2006 @@ -36,6 +36,8 @@

Extending the base classes

+ Connection and Transaction handling +

Managers and Caching

Beans --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org For additional commands, e-mail: torque-dev-help@db.apache.org