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