View Javadoc

1   /*
2    * $Id: JdbcTransaction.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.jdbc;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.transaction.TransactionException;
15  import org.mule.config.i18n.CoreMessages;
16  import org.mule.transaction.AbstractSingleResourceTransaction;
17  import org.mule.transaction.IllegalTransactionStateException;
18  import org.mule.transaction.TransactionRollbackException;
19  import org.mule.transport.jdbc.i18n.JdbcMessages;
20  
21  import java.sql.Connection;
22  import java.sql.SQLException;
23  
24  import javax.sql.DataSource;
25  
26  /**
27   * TODO
28   */
29  public class JdbcTransaction extends AbstractSingleResourceTransaction
30  {
31  
32      public JdbcTransaction(MuleContext muleContext)
33      {
34          super(muleContext);
35      }
36  
37      public void bindResource(Object key, Object resource) throws TransactionException
38      {
39          if (!(key instanceof DataSource) || !(resource instanceof Connection))
40          {
41              throw new IllegalTransactionStateException(
42                  CoreMessages.transactionCanOnlyBindToResources("javax.sql.DataSource/java.sql.Connection"));
43          }
44          Connection con = (Connection)resource;
45          try
46          {
47              if (con.getAutoCommit())
48              {
49                  con.setAutoCommit(false);
50              }
51          }
52          catch (SQLException e)
53          {
54              throw new TransactionException(JdbcMessages.transactionSetAutoCommitFailed(), e);
55          }
56          super.bindResource(key, resource);
57      }
58  
59      protected void doBegin() throws TransactionException
60      {
61          // Do nothing
62      }
63  
64      protected void doCommit() throws TransactionException
65      {
66          if (resource == null)
67          {
68              logger.warn(CoreMessages.commitTxButNoResource(this));
69              return;
70          }
71          
72          try
73          {
74              ((Connection)resource).commit();
75              ((Connection)resource).close();
76          }
77          catch (SQLException e)
78          {
79              throw new TransactionException(CoreMessages.transactionCommitFailed(), e);
80          }
81      }
82  
83      protected void doRollback() throws TransactionException
84      {
85          if (resource == null)
86          {
87              logger.warn(CoreMessages.rollbackTxButNoResource(this));
88              return;
89          }
90  
91          try
92          {
93              ((Connection)resource).rollback();
94              ((Connection)resource).close();
95          }
96          catch (SQLException e)
97          {
98              throw new TransactionRollbackException(CoreMessages.transactionRollbackFailed(), e);
99          }
100     }
101 }