Save This Page
Home » jboss-5.0.0.CR1-src » org.jboss.resource.adapter » jdbc » [javadoc | source]
    1   /*
    2    * JBoss, Home of Professional Open Source.
    3    * Copyright 2006, Red Hat Middleware LLC, and individual contributors
    4    * as indicated by the @author tags. See the copyright.txt file in the
    5    * distribution for a full listing of individual contributors.
    6    *
    7    * This is free software; you can redistribute it and/or modify it
    8    * under the terms of the GNU Lesser General Public License as
    9    * published by the Free Software Foundation; either version 2.1 of
   10    * the License, or (at your option) any later version.
   11    *
   12    * This software is distributed in the hope that it will be useful,
   13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   15    * Lesser General Public License for more details.
   16    *
   17    * You should have received a copy of the GNU Lesser General Public
   18    * License along with this software; if not, write to the Free
   19    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   20    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   21    */
   22   package org.jboss.resource.adapter.jdbc;
   23   
   24   import java.io.PrintWriter;
   25   import java.io.Serializable;
   26   import java.sql.Connection;
   27   import java.sql.SQLException;
   28   
   29   import javax.naming.Reference;
   30   import javax.resource.Referenceable;
   31   import javax.resource.ResourceException;
   32   import javax.resource.spi.ConnectionManager;
   33   import javax.resource.spi.ConnectionRequestInfo;
   34   import javax.sql.DataSource;
   35   import javax.transaction.RollbackException;
   36   
   37   import org.jboss.resource.connectionmanager.JTATransactionChecker;
   38   import org.jboss.tm.TransactionTimeoutConfiguration;
   39   import org.jboss.util.NestedSQLException;
   40   
   41   /**
   42    * WrapperDataSource
   43    *
   44    * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
   45    * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
   46    * @version $Revision: 71788 $
   47    */
   48   public class WrapperDataSource extends JBossWrapper implements Referenceable, DataSource, Serializable  
   49   {
   50      static final long serialVersionUID = 3570285419164793501L;
   51      
   52      private final BaseWrapperManagedConnectionFactory mcf;
   53      private final ConnectionManager cm;
   54   
   55      private Reference reference;
   56   
   57      protected WrapperDataSource (final BaseWrapperManagedConnectionFactory mcf, final ConnectionManager cm)
   58      {
   59         this.mcf = mcf;
   60         this.cm = cm;   
   61      }
   62   
   63      public PrintWriter getLogWriter() throws SQLException
   64      {
   65         // TODO: implement this javax.sql.DataSource method
   66         return null;
   67      }
   68   
   69      public void setLogWriter(PrintWriter param1) throws SQLException
   70      {
   71         // TODO: implement this javax.sql.DataSource method
   72      }
   73   
   74      public int getLoginTimeout() throws SQLException
   75      {
   76         // TODO: implement this javax.sql.DataSource method
   77         return 0;
   78      }
   79   
   80      public void setLoginTimeout(int param1) throws SQLException
   81      {
   82         // TODO: implement this javax.sql.DataSource method
   83      }
   84      
   85      public Connection getConnection() throws SQLException
   86      {
   87         try 
   88         {
   89            WrappedConnection wc = (WrappedConnection) cm.allocateConnection(mcf, null);
   90            wc.setDataSource(this);
   91            return wc;
   92         }
   93         catch (ResourceException re)
   94         {
   95            throw new NestedSQLException(re);
   96         }
   97      }
   98   
   99      public Connection getConnection(String user, String password) throws SQLException
  100      {
  101         ConnectionRequestInfo cri = new WrappedConnectionRequestInfo(user, password);
  102         try 
  103         {
  104            WrappedConnection wc = (WrappedConnection) cm.allocateConnection(mcf, cri);
  105            wc.setDataSource(this);
  106            return wc;
  107         }
  108         catch (ResourceException re)
  109         {
  110            throw new NestedSQLException(re);
  111         }
  112      }
  113   
  114      public void setReference(final Reference reference)
  115      {
  116         this.reference = reference;
  117      }
  118   
  119      public Reference getReference()
  120      {
  121         return reference;
  122      }
  123      
  124      protected int getTimeLeftBeforeTransactionTimeout() throws SQLException
  125      {
  126         try
  127         {
  128            if (cm instanceof TransactionTimeoutConfiguration)
  129            {
  130               long timeout = ((TransactionTimeoutConfiguration) cm).getTimeLeftBeforeTransactionTimeout(true);
  131               // No timeout
  132               if (timeout == -1)
  133                  return -1;
  134               // Round up to the nearest second
  135               long result = timeout / 1000;
  136               if ((result % 1000) != 0)
  137                  ++result;
  138               return (int) result;
  139            }
  140            else
  141               return -1;
  142         }
  143         catch (RollbackException e)
  144         {
  145            throw new NestedSQLException(e);
  146         }
  147      }
  148      
  149      /**
  150       * Check whether a tranasction is active
  151       *
  152       * @throws SQLException if the transaction is not active, preparing, prepared or committing or for any error in the transaction manager
  153       */
  154      protected void checkTransactionActive() throws SQLException
  155      {
  156         if (cm == null)
  157            throw new SQLException("No connection manager");
  158         try
  159         {
  160            if (cm instanceof JTATransactionChecker)
  161               ((JTATransactionChecker) cm).checkTransactionActive();
  162         }
  163         catch (Exception e)
  164         {
  165            throw new NestedSQLException(e);
  166         }
  167      }
  168   }

Save This Page
Home » jboss-5.0.0.CR1-src » org.jboss.resource.adapter » jdbc » [javadoc | source]