Coverage Report - org.mule.transport.jdbc.JdbcTransaction
 
Classes in this File Line Coverage Branch Coverage Complexity
JdbcTransaction
0%
0/31
0%
0/10
3.6
 
 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  0
         super(muleContext);
 35  0
     }
 36  
 
 37  
     public void bindResource(Object key, Object resource) throws TransactionException
 38  
     {
 39  0
         if (!(key instanceof DataSource) || !(resource instanceof Connection))
 40  
         {
 41  0
             throw new IllegalTransactionStateException(
 42  
                 CoreMessages.transactionCanOnlyBindToResources("javax.sql.DataSource/java.sql.Connection"));
 43  
         }
 44  0
         Connection con = (Connection)resource;
 45  
         try
 46  
         {
 47  0
             if (con.getAutoCommit())
 48  
             {
 49  0
                 con.setAutoCommit(false);
 50  
             }
 51  
         }
 52  0
         catch (SQLException e)
 53  
         {
 54  0
             throw new TransactionException(JdbcMessages.transactionSetAutoCommitFailed(), e);
 55  0
         }
 56  0
         super.bindResource(key, resource);
 57  0
     }
 58  
 
 59  
     protected void doBegin() throws TransactionException
 60  
     {
 61  
         // Do nothing
 62  0
     }
 63  
 
 64  
     protected void doCommit() throws TransactionException
 65  
     {
 66  0
         if (resource == null)
 67  
         {
 68  0
             logger.warn(CoreMessages.commitTxButNoResource(this));
 69  0
             return;
 70  
         }
 71  
         
 72  
         try
 73  
         {
 74  0
             ((Connection)resource).commit();
 75  0
             ((Connection)resource).close();
 76  
         }
 77  0
         catch (SQLException e)
 78  
         {
 79  0
             throw new TransactionException(CoreMessages.transactionCommitFailed(), e);
 80  0
         }
 81  0
     }
 82  
 
 83  
     protected void doRollback() throws TransactionException
 84  
     {
 85  0
         if (resource == null)
 86  
         {
 87  0
             logger.warn(CoreMessages.rollbackTxButNoResource(this));
 88  0
             return;
 89  
         }
 90  
 
 91  
         try
 92  
         {
 93  0
             ((Connection)resource).rollback();
 94  0
             ((Connection)resource).close();
 95  
         }
 96  0
         catch (SQLException e)
 97  
         {
 98  0
             throw new TransactionRollbackException(CoreMessages.transactionRollbackFailed(), e);
 99  0
         }
 100  0
     }
 101  
 }