Coverage Report - org.mule.transport.jdbc.JdbcTransaction
 
Classes in this File Line Coverage Branch Coverage Complexity
JdbcTransaction
55%
17/31
50%
5/10
3.6
 
 1  
 /*
 2  
  * $Id: JdbcTransaction.java 12181 2008-06-26 20:05:55Z dirk.olmes $
 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 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.transaction.TransactionException;
 14  
 import org.mule.config.i18n.CoreMessages;
 15  
 import org.mule.transaction.AbstractSingleResourceTransaction;
 16  
 import org.mule.transaction.IllegalTransactionStateException;
 17  
 import org.mule.transaction.TransactionRollbackException;
 18  
 import org.mule.transport.jdbc.i18n.JdbcMessages;
 19  
 
 20  
 import java.sql.Connection;
 21  
 import java.sql.SQLException;
 22  
 
 23  
 import javax.sql.DataSource;
 24  
 
 25  
 /**
 26  
  * TODO
 27  
  */
 28  
 public class JdbcTransaction extends AbstractSingleResourceTransaction
 29  
 {
 30  
 
 31  
     public JdbcTransaction()
 32  
     {
 33  10
         super();
 34  10
     }
 35  
 
 36  
     public void bindResource(Object key, Object resource) throws TransactionException
 37  
     {
 38  8
         if (!(key instanceof DataSource) || !(resource instanceof Connection))
 39  
         {
 40  0
             throw new IllegalTransactionStateException(
 41  
                 CoreMessages.transactionCanOnlyBindToResources("javax.sql.DataSource/java.sql.Connection"));
 42  
         }
 43  8
         Connection con = (Connection)resource;
 44  
         try
 45  
         {
 46  8
             if (con.getAutoCommit())
 47  
             {
 48  8
                 con.setAutoCommit(false);
 49  
             }
 50  
         }
 51  0
         catch (SQLException e)
 52  
         {
 53  0
             throw new TransactionException(JdbcMessages.transactionSetAutoCommitFailed(), e);
 54  8
         }
 55  8
         super.bindResource(key, resource);
 56  8
     }
 57  
 
 58  
     protected void doBegin() throws TransactionException
 59  
     {
 60  
         // Do nothing
 61  10
     }
 62  
 
 63  
     protected void doCommit() throws TransactionException
 64  
     {
 65  10
         if (resource == null)
 66  
         {
 67  2
             logger.warn(CoreMessages.noBindingResource());
 68  2
             return;
 69  
         }
 70  
         
 71  
         try
 72  
         {
 73  8
             ((Connection)resource).commit();
 74  8
             ((Connection)resource).close();
 75  
         }
 76  0
         catch (SQLException e)
 77  
         {
 78  0
             throw new TransactionException(CoreMessages.transactionCommitFailed(), e);
 79  8
         }
 80  8
     }
 81  
 
 82  
     protected void doRollback() throws TransactionException
 83  
     {
 84  0
         if (resource == null)
 85  
         {
 86  0
             logger.warn(CoreMessages.noBindingResource());
 87  0
             return;
 88  
         }
 89  
 
 90  
         try
 91  
         {
 92  0
             ((Connection)resource).rollback();
 93  0
             ((Connection)resource).close();
 94  
         }
 95  0
         catch (SQLException e)
 96  
         {
 97  0
             throw new TransactionRollbackException(CoreMessages.transactionRollbackFailed(), e);
 98  0
         }
 99  0
     }
 100  
 }