commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmcna...@apache.org
Subject cvs commit: jakarta-commons/dbcp/src/test/org/apache/commons/dbcp/datasources TestPerUserPoolDataSource.java TestSharedPoolDataSource.java
Date Mon, 25 Aug 2003 17:08:52 GMT
jmcnally    2003/08/25 10:08:52

  Modified:    dbcp/src/java/org/apache/commons/dbcp/datasources
                        InstanceKeyDataSource.java
                        PerUserPoolDataSource.java
                        SharedPoolDataSource.java
               dbcp/src/test/org/apache/commons/dbcp/datasources
                        TestPerUserPoolDataSource.java
                        TestSharedPoolDataSource.java
  Log:
  Added default transaction isolation.  Original patch was from Dan Price
  against the old Jdbc2Pool code.
  Also removed synchronized from registerPool as it is private and always
  called from a synchronized method.
  
  Revision  Changes    Path
  1.5       +46 -4     jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/datasources/InstanceKeyDataSource.java
  
  Index: InstanceKeyDataSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/datasources/InstanceKeyDataSource.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- InstanceKeyDataSource.java	22 Aug 2003 16:08:32 -0000	1.4
  +++ InstanceKeyDataSource.java	25 Aug 2003 17:08:51 -0000	1.5
  @@ -136,6 +136,12 @@
       private static final String GET_CONNECTION_CALLED 
               = "A Connection was already requested from this source, " 
               + "further initialization is not allowed.";
  +    private static final String BAD_TRANSACTION_ISOLATION
  +        = "The requested TransactionIsolation level is invalid.";
  +    /**
  +    * Internal constant to indicate the level is not set. 
  +    */
  +    protected static final int UNKNOWN_TRANSACTIONISOLATION = -1;
   
       private boolean getConnectionCalled = false;
   
  @@ -143,6 +149,7 @@
       /** DataSource Name used to find the ConnectionPoolDataSource */
       private String dataSourceName = null;
       private boolean defaultAutoCommit = false;
  +    private int defaultTransactionIsolation = UNKNOWN_TRANSACTIONISOLATION;
       private int maxActive = GenericObjectPool.DEFAULT_MAX_ACTIVE;
       private int maxIdle = GenericObjectPool.DEFAULT_MAX_IDLE;
       private int maxWait = (int)Math.min((long)Integer.MAX_VALUE,
  @@ -312,6 +319,41 @@
       public void setDefaultReadOnly(boolean v) {
           assertInitializationAllowed();
           this.defaultReadOnly = v;
  +    }
  +
  +    /**
  +     * Get the value of defaultTransactionIsolation, which defines the state of
  +     * connections handed out from this pool.  The value can be changed
  +     * on the Connection using Connection.setTransactionIsolation(int).
  +     * If this method returns -1, the default is JDBC driver dependent.
  +     * 
  +     * @return value of defaultTransactionIsolation.
  +     */
  +    public int getDefaultTransactionIsolation() {
  +            return defaultTransactionIsolation;
  +    }
  +
  +    /**
  +     * Set the value of defaultTransactionIsolation, which defines the state of
  +     * connections handed out from this pool.  The value can be changed
  +     * on the Connection using Connection.setTransactionIsolation(int).
  +     * The default is JDBC driver dependent.
  +     * 
  +     * @param v  Value to assign to defaultTransactionIsolation
  +     */
  +    public void setDefaultTransactionIsolation(int v) {
  +        assertInitializationAllowed();
  +        switch (v) {
  +        case Connection.TRANSACTION_NONE:
  +        case Connection.TRANSACTION_READ_COMMITTED:
  +        case Connection.TRANSACTION_READ_UNCOMMITTED:
  +        case Connection.TRANSACTION_REPEATABLE_READ:
  +        case Connection.TRANSACTION_SERIALIZABLE:
  +            break;
  +        default:
  +            throw new IllegalArgumentException(BAD_TRANSACTION_ISOLATION);
  +        }
  +        this.defaultTransactionIsolation = v;
       }
       
       /**
  
  
  
  1.4       +42 -5     jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/datasources/PerUserPoolDataSource.java
  
  Index: PerUserPoolDataSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/datasources/PerUserPoolDataSource.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- PerUserPoolDataSource.java	22 Aug 2003 16:08:32 -0000	1.3
  +++ PerUserPoolDataSource.java	25 Aug 2003 17:08:51 -0000	1.4
  @@ -98,6 +98,7 @@
       private int defaultMaxWait = (int)Math.min((long)Integer.MAX_VALUE,
           GenericObjectPool.DEFAULT_MAX_WAIT);
       private Map perUserDefaultAutoCommit = null;    
  +    private Map perUserDefaultTransactionIsolation = null;
       private Map perUserMaxActive = null;    
       private Map perUserMaxIdle = null;    
       private Map perUserMaxWait = null;
  @@ -225,7 +226,32 @@
           }
           perUserDefaultAutoCommit.put(username, value);
       }
  -    
  +
  +    /**
  +     * The isolation level of connections when returned from getConnection.  
  +     * If null, the username will use the value of defaultTransactionIsolation.
  +     */
  +    public Integer getPerUserDefaultTransactionIsolation(String username) {
  +        Integer value = null;
  +        if (perUserDefaultTransactionIsolation != null) {
  +            value = (Integer) perUserDefaultTransactionIsolation.get(username);
  +        }
  +        return value;
  +    }
  +
  +    /**
  +     * The isolation level of connections when returned from getConnection.  
  +     * Valid values are the constants defined in Connection.
  +     */
  +    public void setPerUserDefaultTransactionIsolation(String username, 
  +                                                      Integer value) {
  +        assertInitializationAllowed();
  +        if (perUserDefaultTransactionIsolation == null) {
  +            perUserDefaultTransactionIsolation = new HashMap();
  +        }
  +        perUserDefaultTransactionIsolation.put(username, value);
  +    }
  +
       /**
        * The maximum number of active connections that can be allocated from
        * this pool at the same time, or zero for no limit.
  @@ -421,8 +447,19 @@
               }
           }    
   
  +        int defaultTransactionIsolation = getDefaultTransactionIsolation();
  +        if (username != null) {
  +            Integer userMax = getPerUserDefaultTransactionIsolation(username);
  +            if (userMax != null) {
  +                defaultTransactionIsolation = userMax.intValue();
  +            }
  +        }
  +
           con.setAutoCommit(defaultAutoCommit);
           con.setReadOnly(defaultReadOnly);
  +        if (defaultTransactionIsolation != UNKNOWN_TRANSACTIONISOLATION) {
  +            con.setTransactionIsolation(defaultTransactionIsolation);
  +        }
       }
   
       private PoolKey getPoolKey(String username) {
  
  
  
  1.4       +9 -5      jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/datasources/SharedPoolDataSource.java
  
  Index: SharedPoolDataSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbcp/src/java/org/apache/commons/dbcp/datasources/SharedPoolDataSource.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SharedPoolDataSource.java	22 Aug 2003 16:08:32 -0000	1.3
  +++ SharedPoolDataSource.java	25 Aug 2003 17:08:51 -0000	1.4
  @@ -227,7 +227,7 @@
           return key;
       }
   
  -    private synchronized void registerPool(
  +    private void registerPool(
           String username, String password) 
           throws javax.naming.NamingException, SQLException {
   
  @@ -257,6 +257,10 @@
           throws SQLException {
           con.setAutoCommit(isDefaultAutoCommit());
           con.setReadOnly(isDefaultReadOnly());
  +        int defaultTransactionIsolation = getDefaultTransactionIsolation();
  +        if (defaultTransactionIsolation != UNKNOWN_TRANSACTIONISOLATION) {
  +            con.setTransactionIsolation(defaultTransactionIsolation);
  +        }
       }
   }
   
  
  
  
  1.4       +25 -5     jakarta-commons/dbcp/src/test/org/apache/commons/dbcp/datasources/TestPerUserPoolDataSource.java
  
  Index: TestPerUserPoolDataSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbcp/src/test/org/apache/commons/dbcp/datasources/TestPerUserPoolDataSource.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestPerUserPoolDataSource.java	22 Aug 2003 16:08:32 -0000	1.3
  +++ TestPerUserPoolDataSource.java	25 Aug 2003 17:08:52 -0000	1.4
  @@ -107,6 +107,8 @@
           tds.setDefaultMaxWait((int)(getMaxWait()));
           tds.setPerUserMaxActive("foo",new Integer(getMaxActive()));
           tds.setPerUserMaxWait("foo",new Integer((int)(getMaxWait())));
  +        tds.setDefaultTransactionIsolation(
  +            Connection.TRANSACTION_READ_COMMITTED);
   
           ds = tds;
       }
  @@ -484,5 +486,23 @@
               return state;
           }
       }
  -    
  +
  +    public void testTransactionIsolationBehavior() throws Exception {
  +        Connection conn = getConnection();
  +        assertTrue(conn != null);
  +        assertEquals(Connection.TRANSACTION_READ_COMMITTED, 
  +                     conn.getTransactionIsolation());
  +        conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
  +        conn.close();
  +        
  +        Connection conn2 = getConnection();
  +        assertEquals(Connection.TRANSACTION_READ_COMMITTED, 
  +                     conn2.getTransactionIsolation());
  +        
  +        Connection conn3 = getConnection();
  +        assertEquals(Connection.TRANSACTION_READ_COMMITTED, 
  +                     conn3.getTransactionIsolation());
  +        conn2.close();
  +        conn3.close();
  +    }     
   }
  
  
  
  1.4       +25 -4     jakarta-commons/dbcp/src/test/org/apache/commons/dbcp/datasources/TestSharedPoolDataSource.java
  
  Index: TestSharedPoolDataSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/dbcp/src/test/org/apache/commons/dbcp/datasources/TestSharedPoolDataSource.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestSharedPoolDataSource.java	22 Aug 2003 16:08:32 -0000	1.3
  +++ TestSharedPoolDataSource.java	25 Aug 2003 17:08:52 -0000	1.4
  @@ -105,6 +105,8 @@
           tds.setConnectionPoolDataSource(pcds);
           tds.setMaxActive(getMaxActive());
           tds.setMaxWait((int)(getMaxWait()));
  +        tds.setDefaultTransactionIsolation(
  +            Connection.TRANSACTION_READ_COMMITTED);
   
           ds = tds;
       }
  @@ -433,4 +435,23 @@
               return state;
           }
       }
  +
  +    public void testTransactionIsolationBehavior() throws Exception {
  +        Connection conn = getConnection();
  +        assertTrue(conn != null);
  +        assertEquals(Connection.TRANSACTION_READ_COMMITTED, 
  +                     conn.getTransactionIsolation());
  +        conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
  +        conn.close();
  +        
  +        Connection conn2 = getConnection();
  +        assertEquals(Connection.TRANSACTION_READ_COMMITTED, 
  +                     conn2.getTransactionIsolation());
  +        
  +        Connection conn3 = getConnection();
  +        assertEquals(Connection.TRANSACTION_READ_COMMITTED, 
  +                     conn3.getTransactionIsolation());
  +        conn2.close();
  +        conn3.close();
  +    }     
   }
  
  
  

Mime
View raw message