commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From di...@apache.org
Subject cvs commit: jakarta-commons/dbcp/src/java/org/apache/commons/dbcp BasicDataSource.java
Date Sun, 25 Jan 2004 19:57:38 GMT
dirkv       2004/01/25 11:57:38

  Modified:    dbcp/src/java/org/apache/commons/dbcp BasicDataSource.java
  Log:
  First step towards dynamic reconfiguration of BasicDataSource
  
  Revision  Changes    Path
  1.32      +131 -56   jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/BasicDataSource.java
  
  Index: BasicDataSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/BasicDataSource.java,v
  retrieving revision 1.31
  retrieving revision 1.32
  diff -u -r1.31 -r1.32
  --- BasicDataSource.java	10 Nov 2003 14:51:43 -0000	1.31
  +++ BasicDataSource.java	25 Jan 2004 19:57:38 -0000	1.32
  @@ -85,7 +85,6 @@
    * @author Dirk Verbeeck
    * @version $Revision$ $Date$
    */
  -
   public class BasicDataSource implements DataSource {
   
       // ------------------------------------------------------------- Properties
  @@ -95,12 +94,13 @@
        */
       protected boolean defaultAutoCommit = true;
   
  -    public boolean getDefaultAutoCommit() {
  +    public synchronized boolean getDefaultAutoCommit() {
           return this.defaultAutoCommit;
       }
   
  -    public void setDefaultAutoCommit(boolean defaultAutoCommit) {
  +    public synchronized void setDefaultAutoCommit(boolean defaultAutoCommit) {
           this.defaultAutoCommit = defaultAutoCommit;
  +        this.restartNeeded = true;
       }
   
   
  @@ -109,12 +109,13 @@
        */
       protected boolean defaultReadOnly = false;
   
  -    public boolean getDefaultReadOnly() {
  +    public synchronized boolean getDefaultReadOnly() {
           return this.defaultReadOnly;
       }
   
  -    public void setDefaultReadOnly(boolean defaultReadOnly) {
  +    public synchronized void setDefaultReadOnly(boolean defaultReadOnly) {
           this.defaultReadOnly = defaultReadOnly;
  +        this.restartNeeded = true;
       }
   
   
  @@ -123,12 +124,13 @@
        */
       protected int defaultTransactionIsolation = PoolableConnectionFactory.UNKNOWN_TRANSACTIONISOLATION;
   
  -    public int getDefaultTransactionIsolation() {
  +    public synchronized int getDefaultTransactionIsolation() {
           return this.defaultTransactionIsolation;
       }
   
  -    public void setDefaultTransactionIsolation(int defaultTransactionIsolation) {
  +    public synchronized void setDefaultTransactionIsolation(int defaultTransactionIsolation)
{
           this.defaultTransactionIsolation = defaultTransactionIsolation;
  +        this.restartNeeded = true;
       }
   
   
  @@ -137,17 +139,18 @@
        */
       protected String defaultCatalog = null;
   
  -    public String getDefaultCatalog() {
  +    public synchronized String getDefaultCatalog() {
           return this.defaultCatalog;
       }
   
  -    public void setDefaultCatalog(String defaultCatalog) {
  +    public synchronized void setDefaultCatalog(String defaultCatalog) {
           if ((defaultCatalog != null) && (defaultCatalog.trim().length() > 0))
{
               this.defaultCatalog = defaultCatalog;
           }
           else {
               this.defaultCatalog = null;
           }
  +        this.restartNeeded = true;
       }
   
     
  @@ -156,17 +159,18 @@
        */
       protected String driverClassName = null;
   
  -    public String getDriverClassName() {
  +    public synchronized String getDriverClassName() {
           return this.driverClassName;
       }
   
  -    public void setDriverClassName(String driverClassName) {
  +    public synchronized void setDriverClassName(String driverClassName) {
           if ((driverClassName != null) && (driverClassName.trim().length() >
0)) {
               this.driverClassName = driverClassName;
           }
           else {
               this.driverClassName = null;
           }
  +        this.restartNeeded = true;
       }
   
   
  @@ -176,12 +180,15 @@
        */
       protected int maxActive = GenericObjectPool.DEFAULT_MAX_ACTIVE;
   
  -    public int getMaxActive() {
  +    public synchronized int getMaxActive() {
           return this.maxActive;
       }
   
  -    public void setMaxActive(int maxActive) {
  +    public synchronized void setMaxActive(int maxActive) {
           this.maxActive = maxActive;
  +        if (connectionPool != null) {
  +            connectionPool.setMaxActive(maxActive);
  +        }
       }
   
   
  @@ -191,12 +198,15 @@
        */
       protected int maxIdle = GenericObjectPool.DEFAULT_MAX_IDLE;;
   
  -    public int getMaxIdle() {
  +    public synchronized int getMaxIdle() {
           return this.maxIdle;
       }
   
  -    public void setMaxIdle(int maxIdle) {
  +    public synchronized void setMaxIdle(int maxIdle) {
           this.maxIdle = maxIdle;
  +        if (connectionPool != null) {
  +            connectionPool.setMaxIdle(maxIdle);
  +        }
       }
   
       /**
  @@ -205,12 +215,15 @@
        */
       protected int minIdle = GenericObjectPool.DEFAULT_MIN_IDLE;;
   
  -    public int getMinIdle() {
  +    public synchronized int getMinIdle() {
           return this.minIdle;
       }
   
  -    public void setMinIdle(int minIdle) {
  +    public synchronized void setMinIdle(int minIdle) {
          this.minIdle = minIdle;
  +       if (connectionPool != null) {
  +           connectionPool.setMinIdle(minIdle);
  +       }
       }
   
       /**
  @@ -220,12 +233,15 @@
        */
       protected long maxWait = GenericObjectPool.DEFAULT_MAX_WAIT;
   
  -    public long getMaxWait() {
  +    public synchronized long getMaxWait() {
           return this.maxWait;
       }
   
  -    public void setMaxWait(long maxWait) {
  +    public synchronized void setMaxWait(long maxWait) {
           this.maxWait = maxWait;
  +        if (connectionPool != null) {
  +            connectionPool.setMaxWait(maxWait);
  +        }
       }
   
       /**
  @@ -237,7 +253,7 @@
        * Returns true if we are pooling statements.
        * @return boolean
        */
  -    public boolean isPoolPreparedStatements() {
  +    public synchronized boolean isPoolPreparedStatements() {
           return this.poolPreparedStatements;
       }
   
  @@ -245,8 +261,9 @@
        * Sets whether to pool statements or not.
        * @param poolPreparedStatements pooling on or off
        */
  -    public void setPoolPreparedStatements(boolean poolingStatements) {
  -         this.poolPreparedStatements = poolingStatements;
  +    public synchronized void setPoolPreparedStatements(boolean poolingStatements) {
  +        this.poolPreparedStatements = poolingStatements;
  +        this.restartNeeded = true;
       }
   
       /**
  @@ -257,12 +274,13 @@
        */
       protected int maxOpenPreparedStatements = GenericKeyedObjectPool.DEFAULT_MAX_TOTAL;
   
  -    public int getMaxOpenPreparedStatements() {
  +    public synchronized int getMaxOpenPreparedStatements() {
           return this.maxOpenPreparedStatements;
       }
   
  -    public void setMaxOpenPreparedStatements(int maxOpenStatements) {
  +    public synchronized void setMaxOpenPreparedStatements(int maxOpenStatements) {
           this.maxOpenPreparedStatements = maxOpenStatements;
  +        this.restartNeeded = true;
       }
   
       /**
  @@ -272,12 +290,15 @@
        */
       protected boolean testOnBorrow = true;
   
  -    public boolean getTestOnBorrow() {
  +    public synchronized boolean getTestOnBorrow() {
           return this.testOnBorrow;
       }
   
  -    public void setTestOnBorrow(boolean testOnBorrow) {
  +    public synchronized void setTestOnBorrow(boolean testOnBorrow) {
           this.testOnBorrow = testOnBorrow;
  +        if (connectionPool != null) {
  +            connectionPool.setTestOnBorrow(testOnBorrow);
  +        }
       }
   
       /**
  @@ -286,12 +307,15 @@
        */
       protected boolean testOnReturn = false;
   
  -    public boolean getTestOnReturn() {
  +    public synchronized boolean getTestOnReturn() {
           return this.testOnReturn;
       }
   
  -    public void setTestOnReturn(boolean testOnReturn) {
  +    public synchronized void setTestOnReturn(boolean testOnReturn) {
           this.testOnReturn = testOnReturn;
  +        if (connectionPool != null) {
  +            connectionPool.setTestOnReturn(testOnReturn);
  +        }
       }
   
   
  @@ -303,12 +327,15 @@
       protected long timeBetweenEvictionRunsMillis =
           GenericObjectPool.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS;
           
  -    public long getTimeBetweenEvictionRunsMillis() {
  +    public synchronized long getTimeBetweenEvictionRunsMillis() {
           return this.timeBetweenEvictionRunsMillis;
       }
   
  -    public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
  +    public synchronized void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis)
{
           this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
  +        if (connectionPool != null) {
  +            connectionPool.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
  +        }
       }
   
   
  @@ -319,12 +346,15 @@
       protected int numTestsPerEvictionRun =
           GenericObjectPool.DEFAULT_NUM_TESTS_PER_EVICTION_RUN;
   
  -    public int getNumTestsPerEvictionRun() {
  +    public synchronized int getNumTestsPerEvictionRun() {
           return this.numTestsPerEvictionRun;
       }
   
  -    public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
  +    public synchronized void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
           this.numTestsPerEvictionRun = numTestsPerEvictionRun;
  +        if (connectionPool != null) {
  +            connectionPool.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
  +        }
       }
   
   
  @@ -335,12 +365,15 @@
       protected long minEvictableIdleTimeMillis =
           GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS;
   
  -    public long getMinEvictableIdleTimeMillis() {
  +    public synchronized long getMinEvictableIdleTimeMillis() {
           return this.minEvictableIdleTimeMillis;
       }
   
  -    public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
  +    public synchronized void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
{
           this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
  +        if (connectionPool != null) {
  +            connectionPool.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
  +        }
       }
   
       /**
  @@ -350,19 +383,22 @@
        */
       protected boolean testWhileIdle = false;
   
  -    public boolean getTestWhileIdle() {
  +    public synchronized boolean getTestWhileIdle() {
           return this.testWhileIdle;
       }
   
  -    public void setTestWhileIdle(boolean testWhileIdle) {
  +    public synchronized void setTestWhileIdle(boolean testWhileIdle) {
           this.testWhileIdle = testWhileIdle;
  +        if (connectionPool != null) {
  +            connectionPool.setTestWhileIdle(testWhileIdle);
  +        }
       }
   
       /**
        * [Read Only] The current number of active connections that have been
        * allocated from this data source.
        */
  -    public int getNumActive() {
  +    public synchronized int getNumActive() {
           if (connectionPool != null) {
               return connectionPool.getNumActive();
           } else {
  @@ -375,7 +411,7 @@
        * [Read Only] The current number of idle connections that are waiting
        * to be allocated from this data source.
        */
  -    public int getNumIdle() {
  +    public synchronized int getNumIdle() {
           if (connectionPool != null) {
               return connectionPool.getNumIdle();
           } else {
  @@ -390,12 +426,13 @@
        */
       protected String password = null;
   
  -    public String getPassword() {
  +    public synchronized String getPassword() {
           return this.password;
       }
   
  -    public void setPassword(String password) {
  +    public synchronized void setPassword(String password) {
           this.password = password;
  +        this.restartNeeded = true;
       }
   
   
  @@ -405,12 +442,13 @@
        */
       protected String url = null;
   
  -    public String getUrl() {
  +    public synchronized String getUrl() {
           return this.url;
       }
   
  -    public void setUrl(String url) {
  +    public synchronized void setUrl(String url) {
           this.url = url;
  +        this.restartNeeded = true;
       }
   
   
  @@ -420,12 +458,13 @@
        */
       protected String username = null;
   
  -    public String getUsername() {
  +    public synchronized String getUsername() {
           return this.username;
       }
   
  -    public void setUsername(String username) {
  +    public synchronized void setUsername(String username) {
           this.username = username;
  +        this.restartNeeded = true;
       }
   
   
  @@ -437,16 +476,17 @@
        */
       protected String validationQuery = null;
   
  -    public String getValidationQuery() {
  +    public synchronized String getValidationQuery() {
           return this.validationQuery;
       }
   
  -    public void setValidationQuery(String validationQuery) {
  +    public synchronized void setValidationQuery(String validationQuery) {
           if ((validationQuery != null) && (validationQuery.trim().length() >
0)) {
               this.validationQuery = validationQuery;
           } else {
               this.validationQuery = null;
           }
  +        this.restartNeeded = true;
       }
   
       /** 
  @@ -459,7 +499,7 @@
        * 
        * @return true if access to the underlying is allowed, false otherwise.
        */
  -    public boolean isAccessToUnderlyingConnectionAllowed() {
  +    public synchronized boolean isAccessToUnderlyingConnectionAllowed() {
           return this.accessToUnderlyingConnectionAllowed;
       }
   
  @@ -470,18 +510,33 @@
        * 
        * @param allow Access to the underlying connection is granted when true.
        */
  -    public void setAccessToUnderlyingConnectionAllowed(boolean allow) {
  +    public synchronized void setAccessToUnderlyingConnectionAllowed(boolean allow) {
           this.accessToUnderlyingConnectionAllowed = allow;
  +        this.restartNeeded = true;
       }
   
       // ----------------------------------------------------- Instance Variables
   
  +    // TODO: review & make isRestartNeeded() public, restartNeeded protected
  +
  +    private boolean restartNeeded = false;
  +    
  +    /**
  +     * Returns whether or not a restart is needed.
  +     * @return true if a restart is needed
  +     */
  +    private synchronized boolean isRestartNeeded() {
  +        return restartNeeded;
  +    }
   
       /**
        * The object pool that internally manages our connections.
        */
       protected GenericObjectPool connectionPool = null;
  -
  +    
  +    public GenericObjectPool getConnectionPool() {
  +        return connectionPool;
  +    }
   
       /**
        * The connection properties that will be sent to our JDBC driver when
  @@ -491,7 +546,6 @@
        */
       protected Properties connectionProperties = new Properties();
   
  -
       /**
        * The data source we will use to manage connections.  This object should
        * be acquired <strong>ONLY</strong> by calls to the
  @@ -499,7 +553,6 @@
        */
       protected DataSource dataSource = null;
   
  -
       /**
        * The PrintWriter to which log messages should be directed.
        */
  @@ -606,6 +659,7 @@
               abandonedConfig = new AbandonedConfig();
           }
           abandonedConfig.setRemoveAbandoned(removeAbandoned);
  +        this.restartNeeded = true;
       }                                                        
                                                  
       /**
  @@ -630,6 +684,7 @@
               abandonedConfig = new AbandonedConfig();
           }
           abandonedConfig.setRemoveAbandonedTimeout(removeAbandonedTimeout);
  +        this.restartNeeded = true;
       }                                                                  
                                                                
       /**
  @@ -659,6 +714,7 @@
               abandonedConfig = new AbandonedConfig();
           }
           abandonedConfig.setLogAbandoned(logAbandoned);
  +        this.restartNeeded = true;
       }
   
       // --------------------------------------------------------- Public Methods
  @@ -674,8 +730,13 @@
        */
       public void addConnectionProperty(String name, String value) {
           connectionProperties.put(name, value);
  +        this.restartNeeded = true;
       }
   
  +    public void removeConnectionProperty(String name) {
  +        connectionProperties.remove(name);
  +        this.restartNeeded = true;
  +    }
   
       /**
        * Close and release all connections that are currently stored in the
  @@ -683,7 +744,7 @@
        *
        * @exception SQLException if a database error occurs
        */
  -    public void close() throws SQLException {
  +    public synchronized void close() throws SQLException {
           GenericObjectPool oldpool = connectionPool;
           connectionPool = null;
           dataSource = null;
  @@ -790,13 +851,13 @@
           if (username != null) {
               connectionProperties.put("user", username);
           } else {
  -            this.logWriter.println("DBCP DataSource configured without a 'username'");
  +            log("DBCP DataSource configured without a 'username'");
           }
           
           if (password != null) {
               connectionProperties.put("password", password);
           } else {
  -            this.logWriter.println("DBCP DataSource configured without a 'password'");
  +            log("DBCP DataSource configured without a 'password'");
           }
           
           DriverConnectionFactory driverConnectionFactory =
  @@ -831,8 +892,8 @@
           dataSource.setLogWriter(logWriter);
           return (dataSource);
       }
  -    
  -    private void validateConnectionFactory(PoolableConnectionFactory connectionFactory)
throws Exception {
  +
  +    private static void validateConnectionFactory(PoolableConnectionFactory connectionFactory)
throws Exception {
           Connection conn = null;
           try {
               conn = (Connection) connectionFactory.makeObject();
  @@ -842,6 +903,20 @@
           }
           finally {
               connectionFactory.destroyObject(conn);
  +        }
  +    }
  +
  +    private void restart() {
  +        try {
  +            close();
  +        } catch (SQLException e) {
  +            log("Could not restart DataSource, cause: " + e.getMessage());
  +        }
  +    }
  +
  +    private void log(String message) {
  +        if (logWriter != null) {
  +            logWriter.println(message);
           }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message