KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > jdbc > JdbcTransaction


1 /*
2  * $Id: JdbcTransaction.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.jdbc;
12
13 import org.mule.config.i18n.Message;
14 import org.mule.config.i18n.Messages;
15 import org.mule.transaction.AbstractSingleResourceTransaction;
16 import org.mule.transaction.IllegalTransactionStateException;
17 import org.mule.transaction.TransactionRollbackException;
18 import org.mule.umo.TransactionException;
19
20 import javax.sql.DataSource JavaDoc;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.SQLException JavaDoc;
24
25 /**
26  * TODO
27  */

28 public class JdbcTransaction extends AbstractSingleResourceTransaction
29 {
30
31     public JdbcTransaction()
32     {
33         super();
34     }
35
36     /*
37      * (non-Javadoc)
38      *
39      * @see org.mule.umo.UMOTransaction#bindResource(java.lang.Object,
40      * java.lang.Object)
41      */

42     public void bindResource(Object JavaDoc key, Object JavaDoc resource) throws TransactionException
43     {
44         if (!(key instanceof DataSource JavaDoc) || !(resource instanceof Connection JavaDoc))
45         {
46             throw new IllegalTransactionStateException(new Message(
47                 Messages.TX_CAN_ONLY_BIND_TO_X_TYPE_RESOURCES, "javax.sql.DataSource/java.sql.Connection"));
48         }
49         Connection JavaDoc con = (Connection JavaDoc)resource;
50         try
51         {
52             if (con.getAutoCommit())
53             {
54                 con.setAutoCommit(false);
55             }
56         }
57         catch (SQLException JavaDoc e)
58         {
59             throw new TransactionException(new Message(Messages.TX_SET_AUTO_COMMIT_FAILED), e);
60         }
61         super.bindResource(key, resource);
62     }
63
64     /*
65      * (non-Javadoc)
66      *
67      * @see org.mule.transaction.AbstractSingleResourceTransaction#doBegin()
68      */

69     protected void doBegin() throws TransactionException
70     {
71         // Do nothing
72
}
73
74     /*
75      * (non-Javadoc)
76      *
77      * @see org.mule.transaction.AbstractSingleResourceTransaction#doCommit()
78      */

79     protected void doCommit() throws TransactionException
80     {
81         try
82         {
83             ((Connection JavaDoc)resource).commit();
84             ((Connection JavaDoc)resource).close();
85         }
86         catch (SQLException JavaDoc e)
87         {
88             throw new TransactionException(new Message(Messages.TX_COMMIT_FAILED), e);
89         }
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.mule.transaction.AbstractSingleResourceTransaction#doRollback()
96      */

97     protected void doRollback() throws TransactionException
98     {
99         try
100         {
101             ((Connection JavaDoc)resource).rollback();
102             ((Connection JavaDoc)resource).close();
103         }
104         catch (SQLException JavaDoc e)
105         {
106             throw new TransactionRollbackException(new Message(Messages.TX_ROLLBACK_FAILED), e);
107         }
108     }
109
110 }
111
Popular Tags