You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2002/03/08 21:23:01 UTC

cvs commit: jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/jdbc ConnectionWrapper.java DriverDataSource.java

baliuka     02/03/08 12:23:01

  Modified:    simplestore/src/java/org/apache/commons/simplestore/jdbc
                        DriverDataSource.java
  Added:       simplestore/src/java/org/apache/commons/simplestore/jdbc
                        ConnectionWrapper.java
  Log:
  Fixed Datasource implementation for jdk1.4
  
  Revision  Changes    Path
  1.5       +39 -149   jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/jdbc/DriverDataSource.java
  
  Index: DriverDataSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/jdbc/DriverDataSource.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- DriverDataSource.java	16 Feb 2002 14:26:05 -0000	1.4
  +++ DriverDataSource.java	8 Mar 2002 20:23:01 -0000	1.5
  @@ -55,6 +55,8 @@
   package org.apache.commons.simplestore.jdbc;
   
   import java.io.PrintWriter;
  +import java.lang.reflect.InvocationHandler;
  +import java.lang.reflect.Proxy;
   import java.sql.CallableStatement;
   import java.sql.Connection;
   import java.sql.DatabaseMetaData;
  @@ -69,10 +71,11 @@
   
   import javax.sql.DataSource;
   
  +
   /**
    *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
    *      baliuka@mwm.lt</a>
  - *@version    $Id: DriverDataSource.java,v 1.4 2002/02/16 14:26:05 baliuka Exp $
  + *@version    $Id: DriverDataSource.java,v 1.5 2002/03/08 20:23:01 baliuka Exp $
    *
    */
   public class DriverDataSource implements DataSource {
  @@ -275,165 +278,52 @@
       private ConnectionWrapper newConnection() throws SQLException {
           Connection con = driver.connect(url, properties);
           con.setAutoCommit(false);
  -        return new ConnectionWrapper(con,this);
  +        return ConnectionProxy.create(con,this);
       }
  -    
  -}
   
  -/**
  - *@author     Juozas Baliuka
  - *@version    $Id: DriverDataSource.java,v 1.4 2002/02/16 14:26:05 baliuka Exp $
  - */
  -class ConnectionWrapper implements Connection {
  +}
  + 
  +class ConnectionProxy  implements InvocationHandler {
       
       
       boolean used = true;
       private Connection connection;
       private javax.sql.DataSource ds;
       
  -    public ConnectionWrapper( Connection connection, javax.sql.DataSource ds ) {
  +     ConnectionProxy( Connection connection, javax.sql.DataSource ds ) {
           this.connection = connection;
           this.ds = ds;
           
       }
  -    
  -    public void setUsed(boolean used) {
  -        this.used = used;
  -        
  -    }
  -    
  -    public void setAutoCommit(boolean autoCommit) throws SQLException {
  -        connection.setAutoCommit(autoCommit);
  -    }
  -    
  -    public void setReadOnly(boolean readOnly) throws SQLException {
  -        connection.setReadOnly(readOnly);
  -    }
  -    
  -    public void setCatalog(String catalog) throws SQLException {
  -        connection.setCatalog(catalog);
  -    }
  -    
  -    public void setTransactionIsolation(int level) throws SQLException {
  -        connection.setTransactionIsolation(level);
  -    }
  -    
  -    public void setTypeMap(Map map) throws SQLException {
  -        connection.setTypeMap(map);
  -    }
  -    
  -    public boolean isUsed() {
  -        return used;
  -    }
  -    
  -    public boolean getAutoCommit() throws SQLException {
  -        return connection.getAutoCommit();
  -    }
  -    
  -    
  -    public boolean isClosed() throws SQLException {
  -        if (connection == null) {
  -            return true;
  -        }
  -        try {
  -            Statement stmt = connection.createStatement();
  -            ResultSet rs = stmt.executeQuery("SELECT 1");
  -            rs.next();
  -            rs.getInt(1);
  -            rs.close();
  -            stmt.close();
  -            
  -        } catch (Exception sqle) {
  -            try {
  -                connection.close();
  -            } catch (Exception ignore) {
  -            }
  -            
  -            return true;
  -        }
  -        
  -        return connection.isClosed();
  -    }
  -    
  -    public DatabaseMetaData getMetaData() throws SQLException {
  -        return connection.getMetaData();
  -    }
  -    
  -    public boolean isReadOnly() throws SQLException {
  -        return connection.isReadOnly();
  -    }
  -    
  -    public String getCatalog() throws SQLException {
  -        return connection.getCatalog();
  -    }
  -    
  -    public int getTransactionIsolation() throws SQLException {
  -        return connection.getTransactionIsolation();
  -    }
  -    
  -    public SQLWarning getWarnings() throws SQLException {
  -        return connection.getWarnings();
  -    }
  -    
  -    public Map getTypeMap() throws SQLException {
  -        return connection.getTypeMap();
  -    }
  -    
  -    public void release() throws SQLException {
  -        connection.close();
  -    }
  -    
  -    public void close() throws SQLException {
  -        setUsed(false);
  -       synchronized( ds ){ 
  -        ds.notifyAll();
  -       }  
  -    }
  -    
  -    
  -    public Statement createStatement() throws SQLException {
  -        return connection.createStatement();
  -    }
  -    
  -    public PreparedStatement prepareStatement(String sql) throws SQLException {
  -        return connection.prepareStatement(sql);
  -    }
  -    
  -    public CallableStatement prepareCall(String sql) throws SQLException {
  -        return connection.prepareCall(sql);
  -    }
  -    
  -    public String nativeSQL(String sql) throws SQLException {
  -        return connection.nativeSQL(sql);
  -    }
  -    
  -    public void commit() throws SQLException {
  -        connection.commit();
  -        
  -    }
  -    
  -    public void rollback() throws SQLException {
  -        
  -        connection.rollback();
  -    }
  -    
  -    public void clearWarnings() throws SQLException {
  -        connection.clearWarnings();
  -    }
  -    
  -    public Statement createStatement(int resultSetType, int resultSetConcurrency)
  -    throws SQLException {
  -        return connection.createStatement(resultSetType, resultSetConcurrency);
  -    }
  -    
  -    public PreparedStatement prepareStatement(String sql, int resultSetType,
  -    int resultSetConcurrency) throws SQLException {
  -        return connection.prepareStatement(sql, resultSetType, resultSetConcurrency);
  -    }
  -    
  -    public CallableStatement prepareCall(String sql, int resultSetType,
  -    int resultSetConcurrency) throws SQLException {
  -        return connection.prepareCall(sql, resultSetType, resultSetConcurrency);
  -    }
  +    public Object invoke(Object arg0,java.lang.reflect.Method arg1,Object[] arg2)
  +      throws Throwable{
  +       
  +         if( arg1.getName().equals("close")  ) {
  +           used = false;
  +            synchronized( ds ){ 
  +               ds.notifyAll();
  +             }  
  + 
  +           return null;     
  +         }else if (arg1.getName().equals("release")){
  +             connection.close();
  +             return null;     
  +         }else if (arg1.getName().equals("isUsed")){
  +            return new Boolean(used);
  +         }else if (arg1.getName().equals("setUsed")){
  +            used = ((Boolean) arg2[0] ).booleanValue();
  +            return null;
  +         }else    return arg1.invoke(connection,arg2) ;
  +    }
  +   public static  ConnectionWrapper create(Connection connection, javax.sql.DataSource ds){
  +       return (ConnectionWrapper)
  +     java.lang.reflect.Proxy.newProxyInstance(
  +      Thread.currentThread().getContextClassLoader(),
  +      new Class[]{ConnectionWrapper.class},
  +      new ConnectionProxy(connection,ds)
  +     );        
  +   
  +   }
  +   
   }
   
  
  
  
  1.1                  jakarta-commons-sandbox/simplestore/src/java/org/apache/commons/simplestore/jdbc/ConnectionWrapper.java
  
  Index: ConnectionWrapper.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache Cocoon" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.simplestore.jdbc;
  
  
  import java.sql.Connection;
  
  
  /**
   *@author     Juozas Baliuka <a href="mailto:baliuka@mwm.lt">
   *      baliuka@mwm.lt</a>
   *@version    $Id: ConnectionWrapper.java,v 1.1 2002/03/08 20:23:01 baliuka Exp $
   *
   */
  public interface ConnectionWrapper extends Connection {
  
      public boolean isUsed();
      public void   release();
      public void    setUsed(boolean flag);
     
  
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>