格式化代码

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@48 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-05-13 07:06:11 +00:00
parent b5c1cfd758
commit a568a084b3
14 changed files with 156 additions and 155 deletions
@@ -515,14 +515,14 @@ public class DruidDataSource extends DruidDataAbstractSource implements DruidDat
if (timeBetweenEvictionRunsMillis > 0) {
Thread.sleep(timeBetweenEvictionRunsMillis);
}
ConnectionHolder first = null;
lock.lock();
try {
if (count <= 0) {
continue;
}
if (count <= maxIdle) {
highWater.await();
continue;
@@ -416,7 +416,7 @@ public class PoolableConnection implements PooledConnection, Connection {
if (holder == null) {
return true;
}
return conn.isClosed();
}
@@ -24,37 +24,39 @@ import java.sql.SQLException;
/**
* An implementation of XAConnectionFactory which uses a real XADataSource to obtain connections and XAResources.
*
*
* @author Dain Sundstrom
* @version $Revision$
*/
public class DataSourceXAConnectionFactory implements XAConnectionFactory {
protected TransactionRegistry transactionRegistry;
protected XADataSource xaDataSource;
protected String username;
protected String password;
protected XADataSource xaDataSource;
protected String username;
protected String password;
/**
* Creates an DataSourceXAConnectionFactory which uses the specified XADataSource to create database
* connections. The connections are enlisted into transactions using the specified transaction manager.
*
* Creates an DataSourceXAConnectionFactory which uses the specified XADataSource to create database connections.
* The connections are enlisted into transactions using the specified transaction manager.
*
* @param transactionManager the transaction manager in which connections will be enlisted
* @param xaDataSource the data source from which connections will be retrieved
*/
public DataSourceXAConnectionFactory(TransactionManager transactionManager, XADataSource xaDataSource) {
public DataSourceXAConnectionFactory(TransactionManager transactionManager, XADataSource xaDataSource){
this(transactionManager, xaDataSource, null, null);
}
/**
* Creates an DataSourceXAConnectionFactory which uses the specified XADataSource to create database
* connections. The connections are enlisted into transactions using the specified transaction manager.
*
* Creates an DataSourceXAConnectionFactory which uses the specified XADataSource to create database connections.
* The connections are enlisted into transactions using the specified transaction manager.
*
* @param transactionManager the transaction manager in which connections will be enlisted
* @param xaDataSource the data source from which connections will be retrieved
* @param username the username used for authenticating new connections or null for unauthenticated
* @param password the password used for authenticating new connections
*/
public DataSourceXAConnectionFactory(TransactionManager transactionManager, XADataSource xaDataSource, String username, String password) {
public DataSourceXAConnectionFactory(TransactionManager transactionManager, XADataSource xaDataSource,
String username, String password){
if (transactionManager == null) throw new NullPointerException("transactionManager is null");
if (xaDataSource == null) throw new NullPointerException("xaDataSource is null");
@@ -66,6 +68,7 @@ public class DataSourceXAConnectionFactory implements XAConnectionFactory {
/**
* Gets the username used to authenticate new connections.
*
* @return the user name or null if unauthenticated connections are used
*/
public String getUsername() {
@@ -74,6 +77,7 @@ public class DataSourceXAConnectionFactory implements XAConnectionFactory {
/**
* Sets the username used to authenticate new connections.
*
* @param username the username used for authenticating the connection or null for unauthenticated
*/
public void setUsername(String username) {
@@ -82,6 +86,7 @@ public class DataSourceXAConnectionFactory implements XAConnectionFactory {
/**
* Sets the password used to authenticate new connections.
*
* @param password the password used for authenticating the connection or null for unauthenticated
*/
public void setPassword(String password) {
@@ -111,4 +116,3 @@ public class DataSourceXAConnectionFactory implements XAConnectionFactory {
return connection;
}
}
@@ -124,7 +124,7 @@ public class DruidXADataSource extends DruidDataSource {
// Create the XADataSource instance using the configured class name if it has not been set
if (xaDataSourceInstance == null) {
Class xaDataSourceClass = null;
Class<?> xaDataSourceClass = null;
try {
xaDataSourceClass = Class.forName(xaDataSource);
} catch (Throwable t) {
@@ -46,8 +46,13 @@ public class LocalXAConnectionFactory implements XAConnectionFactory {
* @param connectionFactory the connection factory from which connections will be retrieved
*/
public LocalXAConnectionFactory(TransactionManager transactionManager, ConnectionFactory connectionFactory){
if (transactionManager == null) throw new NullPointerException("transactionManager is null");
if (connectionFactory == null) throw new NullPointerException("connectionFactory is null");
if (transactionManager == null) {
throw new NullPointerException("transactionManager is null");
}
if (connectionFactory == null) {
throw new NullPointerException("connectionFactory is null");
}
this.transactionRegistry = new TransactionRegistry(transactionManager);
this.connectionFactory = connectionFactory;
@@ -154,35 +154,35 @@ public class ManagedConnection extends PoolableConnection {
}
protected void transactionComplete() {
// transactionContext = null;
//
// // if we were using a shared connection, clear the reference now that the transaction has completed
// if (isSharedConnection) {
// // for now, just set the delegate to null, it will be created later if needed
// setDelegate(null);
// isSharedConnection = false;
// }
//
// // if this connection was closed during the transaction and there is still a delegate present close it
// Connection delegate = getDelegateInternal();
// if (isClosed() && delegate != null) {
// try {
// setDelegate(null);
//
// // don't actually close the connection if in a transaction
// if (!delegate.isClosed()) {
// // don't use super.close() because it calls passivate() which marks the
// // the connection as closed without returning it to the pool
// delegate.close();
// }
// } catch (SQLException ignored) {
// // not a whole lot we can do here as connection is closed
// // and this is a transaction callback so there is no
// // way to report the error
// } finally {
// _closed = true;
// }
// }
// transactionContext = null;
//
// // if we were using a shared connection, clear the reference now that the transaction has completed
// if (isSharedConnection) {
// // for now, just set the delegate to null, it will be created later if needed
// setDelegate(null);
// isSharedConnection = false;
// }
//
// // if this connection was closed during the transaction and there is still a delegate present close it
// Connection delegate = getDelegateInternal();
// if (isClosed() && delegate != null) {
// try {
// setDelegate(null);
//
// // don't actually close the connection if in a transaction
// if (!delegate.isClosed()) {
// // don't use super.close() because it calls passivate() which marks the
// // the connection as closed without returning it to the pool
// delegate.close();
// }
// } catch (SQLException ignored) {
// // not a whole lot we can do here as connection is closed
// // and this is a transaction callback so there is no
// // way to report the error
// } finally {
// _closed = true;
// }
// }
throw new RuntimeException("TODO");
}
@@ -18,60 +18,58 @@ package com.alibaba.druid.pool.xa;
import java.sql.Connection;
import java.sql.SQLException;
/**
* The ManagedDataSource is a PoolingDataSource that creates ManagedConnections.
*
*
* @author Dain Sundstrom
* @version $Revision$
*/
public class ManagedDataSource {
private TransactionRegistry transactionRegistry;
/**
* Creates an uninitialized datasource. Before this data source can be used a pool and
* transaction registry must be set.
* Creates an uninitialized datasource. Before this data source can be used a pool and transaction registry must be
* set.
*/
public ManagedDataSource() {
public ManagedDataSource(){
}
/**
* Creates a ManagedDataSource which obtains connections from the specified pool and
* manages them using the specified transaction registry. The TransactionRegistry must
* be the transaction registry obtained from the XAConnectionFactory used to create
* the connection pool. If not an error will occure when attempting to use the connection
* in a global transaction because the XAResource object associated with the connection
* will be unavailable.
*
* Creates a ManagedDataSource which obtains connections from the specified pool and manages them using the
* specified transaction registry. The TransactionRegistry must be the transaction registry obtained from the
* XAConnectionFactory used to create the connection pool. If not an error will occure when attempting to use the
* connection in a global transaction because the XAResource object associated with the connection will be
* unavailable.
*
* @param pool the connection pool
* @param transactionRegistry the transaction registry obtained from the
* XAConnectionFactory used to create the connection pool object factory
* @param transactionRegistry the transaction registry obtained from the XAConnectionFactory used to create the
* connection pool object factory
*/
public ManagedDataSource(TransactionRegistry transactionRegistry) {
public ManagedDataSource(TransactionRegistry transactionRegistry){
this.transactionRegistry = transactionRegistry;
}
/**
* Sets the transaction registry from the XAConnectionFactory used to create the pool.
* The transaction registry can only be set once using either a connector or this setter
* method.
* @param transactionRegistry the transaction registry acquired from the XAConnectionFactory
* used to create the pool
* Sets the transaction registry from the XAConnectionFactory used to create the pool. The transaction registry can
* only be set once using either a connector or this setter method.
*
* @param transactionRegistry the transaction registry acquired from the XAConnectionFactory used to create the pool
*/
public void setTransactionRegistry(TransactionRegistry transactionRegistry) {
if(this.transactionRegistry != null) throw new IllegalStateException("TransactionRegistry already set");
if(transactionRegistry == null) throw new NullPointerException("TransactionRegistry is null");
if (this.transactionRegistry != null) throw new IllegalStateException("TransactionRegistry already set");
if (transactionRegistry == null) throw new NullPointerException("TransactionRegistry is null");
this.transactionRegistry = transactionRegistry;
}
public Connection getConnection() throws SQLException {
// if (_pool == null) throw new IllegalStateException("Pool has not been set");
// if (transactionRegistry == null) throw new IllegalStateException("TransactionRegistry has not been set");
//
// Connection connection = new ManagedConnection(_pool, transactionRegistry, isAccessToUnderlyingConnectionAllowed());
// return connection;
// if (_pool == null) throw new IllegalStateException("Pool has not been set");
// if (transactionRegistry == null) throw new IllegalStateException("TransactionRegistry has not been set");
//
// Connection connection = new ManagedConnection(_pool, transactionRegistry,
// isAccessToUnderlyingConnectionAllowed());
// return connection;
throw new SQLException("TODO"); // TODO
}
}
@@ -26,29 +26,27 @@ import java.sql.SQLException;
import java.lang.ref.WeakReference;
/**
* TransactionContext represents the association between a single XAConnectionFactory and a Transaction.
* This context contains a single shared connection which should be used by all ManagedConnections for
* the XAConnectionFactory, the ability to listen for the transaction completion event, and a method
* to check the status of the transaction.
*
* TransactionContext represents the association between a single XAConnectionFactory and a Transaction. This context
* contains a single shared connection which should be used by all ManagedConnections for the XAConnectionFactory, the
* ability to listen for the transaction completion event, and a method to check the status of the transaction.
*
* @author Dain Sundstrom
* @version $Revision$
*/
public class TransactionContext {
private final TransactionRegistry transactionRegistry;
private final TransactionRegistry transactionRegistry;
private final WeakReference<Transaction> transactionRef;
private Connection sharedConnection;
private Connection sharedConnection;
/**
* Creates a TransactionContext for the specified Transaction and TransactionRegistry. The
* TransactionRegistry is used to obtain the XAResource for the shared connection when it is
* enlisted in the transaction.
*
* @param transactionRegistry the TransactionRegistry used to obtain the XAResource for the
* shared connection
* Creates a TransactionContext for the specified Transaction and TransactionRegistry. The TransactionRegistry is
* used to obtain the XAResource for the shared connection when it is enlisted in the transaction.
*
* @param transactionRegistry the TransactionRegistry used to obtain the XAResource for the shared connection
* @param transaction the transaction
*/
public TransactionContext(TransactionRegistry transactionRegistry, Transaction transaction) {
public TransactionContext(TransactionRegistry transactionRegistry, Transaction transaction){
if (transactionRegistry == null) throw new NullPointerException("transactionRegistry is null");
if (transaction == null) throw new NullPointerException("transaction is null");
this.transactionRegistry = transactionRegistry;
@@ -56,9 +54,9 @@ public class TransactionContext {
}
/**
* Gets the connection shared by all ManagedConnections in the transaction. Specifically,
* connection using the same XAConnectionFactory from which the TransactionRegistry was
* obtained.
* Gets the connection shared by all ManagedConnections in the transaction. Specifically, connection using the same
* XAConnectionFactory from which the TransactionRegistry was obtained.
*
* @return the shared connection for this transaction
*/
public Connection getSharedConnection() {
@@ -66,13 +64,11 @@ public class TransactionContext {
}
/**
* Sets the shared connection for this transaction. The shared connection is enlisted
* in the transaction.
*
* Sets the shared connection for this transaction. The shared connection is enlisted in the transaction.
*
* @param sharedConnection the shared connection
* @throws SQLException if a shared connection is already set, if XAResource for the connection
* could not be found in the transaction registry, or if there was a problem enlisting the
* connection in the transaction
* @throws SQLException if a shared connection is already set, if XAResource for the connection could not be found
* in the transaction registry, or if there was a problem enlisting the connection in the transaction
*/
public void setSharedConnection(Connection sharedConnection) throws SQLException {
if (this.sharedConnection != null) {
@@ -96,13 +92,14 @@ public class TransactionContext {
/**
* Adds a listener for transaction completion events.
*
*
* @param listener the listener to add
* @throws SQLException if a problem occurs adding the listener to the transaction
*/
public void addTransactionContextListener(final TransactionContextListener listener) throws SQLException {
try {
getTransaction().registerSynchronization(new Synchronization() {
public void beforeCompletion() {
}
@@ -120,6 +117,7 @@ public class TransactionContext {
/**
* True if the transaction is active or marked for rollback only.
*
* @return true if the transaction is active or marked for rollback only; false otherwise
* @throws SQLException if a problem occurs obtaining the transaction status
*/
@@ -144,4 +142,3 @@ public class TransactionContext {
return transaction;
}
}
@@ -15,7 +15,6 @@
*/
package com.alibaba.druid.pool.xa;
/**
* A listener for transaction completion events.
*
@@ -26,34 +26,33 @@ import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.xa.XAResource;
/**
* TransactionRegistry tracks Connections and XAResources in a transacted environment for a single XAConnectionFactory.
* </p>
* The TransactionRegistry hides the details of transaction processing from the existing DBCP pooling code, and gives
* the ManagedConnection a way to enlist connections in a transaction, allowing for the maximal rescue of DBCP.
*
* </p> The TransactionRegistry hides the details of transaction processing from the existing DBCP pooling code, and
* gives the ManagedConnection a way to enlist connections in a transaction, allowing for the maximal rescue of DBCP.
*
* @author Dain Sundstrom
* @version $Revision$
*/
public class TransactionRegistry {
private final TransactionManager transactionManager;
private final Map<Transaction, TransactionContext> caches = new WeakHashMap<Transaction, TransactionContext>();
private final Map<Connection, XAResource> xaResources = new WeakHashMap<Connection, XAResource>();
private final TransactionManager transactionManager;
private final Map<Transaction, TransactionContext> caches = new WeakHashMap<Transaction, TransactionContext>();
private final Map<Connection, XAResource> xaResources = new WeakHashMap<Connection, XAResource>();
/**
* Creates a TransactionRegistry for the specified transaction manager.
*
* @param transactionManager the transaction manager used to enlist connections
*/
public TransactionRegistry(TransactionManager transactionManager) {
public TransactionRegistry(TransactionManager transactionManager){
this.transactionManager = transactionManager;
}
/**
* Registers the association between a Connection and a XAResource. When a connection
* is enlisted in a transaction, it is actually the XAResource that is given to the transaction
* manager.
*
* Registers the association between a Connection and a XAResource. When a connection is enlisted in a transaction,
* it is actually the XAResource that is given to the transaction manager.
*
* @param connection the JDBC connection
* @param xaResource the XAResource which managed the connection within a transaction
*/
@@ -65,6 +64,7 @@ public class TransactionRegistry {
/**
* Gets the XAResource registered for the connection.
*
* @param connection the connection
* @return the XAResource registered for the connection; never null
* @throws SQLException if the connection does not have a registered XAResource
@@ -80,6 +80,7 @@ public class TransactionRegistry {
/**
* Gets the active TransactionContext or null if not Transaction is active.
*
* @return the active TransactionContext or null if not Transaction is active
* @throws SQLException if an error occurs while fetching the transaction
*/
@@ -115,10 +116,10 @@ public class TransactionRegistry {
/**
* Unregisters a destroyed connection from {@link TransactionRegistry}
*
* @param connection
*/
public synchronized void unregisterConnection(Connection connection) {
xaResources.remove(connection);
}
}
@@ -21,36 +21,32 @@ import java.sql.SQLException;
import com.alibaba.druid.pool.ConnectionFactory;
/**
* XAConnectionFactory is an extension of ConnectionFactory used to create connections
* in a transaction managed environment. The XAConnectionFactory opperates like a normal
* ConnectionFactory except an TransactionRegistry is provided from which the XAResource
* for a connection can be obtained. This allows the existing DBCP pool code to work with
* XAConnections and gives a the ManagedConnection a way to enlist a connection in the
* the transaction.
*
* XAConnectionFactory is an extension of ConnectionFactory used to create connections in a transaction managed
* environment. The XAConnectionFactory opperates like a normal ConnectionFactory except an TransactionRegistry is
* provided from which the XAResource for a connection can be obtained. This allows the existing DBCP pool code to work
* with XAConnections and gives a the ManagedConnection a way to enlist a connection in the the transaction.
*
* @author Dain Sundstrom
* @author Rodney Waldhoff
* @version $Revision$
*/
public interface XAConnectionFactory extends ConnectionFactory {
/**
* Gets the TransactionRegistry for this connection factory which contains a the
* XAResource for every connection created by this factory.
*
* Gets the TransactionRegistry for this connection factory which contains a the XAResource for every connection
* created by this factory.
*
* @return the transaction registry for this connection factory
*/
TransactionRegistry getTransactionRegistry();
/**
* Create a new {@link java.sql.Connection} in an implementation specific fashion.
* </p>
* An implementation can assume that the caller of this will wrap the connection in
* a proxy that protects access to the setAutoCommit, commit and rollback when
* enrolled in a XA transaction.
*
* Create a new {@link java.sql.Connection} in an implementation specific fashion. </p> An implementation can assume
* that the caller of this will wrap the connection in a proxy that protects access to the setAutoCommit, commit and
* rollback when enrolled in a XA transaction.
*
* @return a new {@link java.sql.Connection}
* @throws java.sql.SQLException if a database error occurs creating the connection
*/
Connection createConnection() throws SQLException;
}
@@ -51,7 +51,7 @@ public class JdbcConnectionStat implements JdbcConnectionStatMBean {
private final AtomicLong closeCount = new AtomicLong(0); // 执行Connection.close的计数
private final AtomicLong commitCount = new AtomicLong(0); // 执行commit的计数
private final AtomicLong rollbackCount = new AtomicLong(0); // 执行rollback的计数
private final AtomicLong connectNanoSpan = new AtomicLong(0); // 连接建立消耗时间总和(纳秒)
private final AtomicLong connectNanoSpan = new AtomicLong(0); // 连接建立消耗时间总和(纳秒)
public void reset() {
concurrentCount.set(0);
@@ -6,23 +6,24 @@ import junit.framework.TestCase;
import com.alibaba.druid.pool.DruidDataSource;
public class DruidDataSourceFilterTest extends TestCase {
public void test_filter() throws Exception {
DruidDataSource dataSource = new DruidDataSource();
Assert.assertEquals(0, dataSource.getFilters().size());
dataSource.setFilters("stat");
Assert.assertEquals(1, dataSource.getFilters().size());
}
public void test_filter_2() throws Exception {
DruidDataSource dataSource = new DruidDataSource();
Assert.assertEquals(0, dataSource.getFilters().size());
dataSource.setFilters("stat,trace");
Assert.assertEquals(2, dataSource.getFilters().size());
}
}
@@ -31,17 +31,17 @@ import com.jolbox.bonecp.BoneCPDataSource;
public class Case0 extends TestCase {
private String jdbcUrl;
private String user;
private String password;
private String driverClass;
private int initialSize = 10;
private int minPoolSize = 1;
private int maxPoolSize = 2;
private int maxActive = 2;
private String validationQuery = null;
public final int LOOP_COUNT = 5;
private String jdbcUrl;
private String user;
private String password;
private String driverClass;
private int initialSize = 10;
private int minPoolSize = 1;
private int maxPoolSize = 2;
private int maxActive = 2;
private String validationQuery = null;
public final int LOOP_COUNT = 5;
protected void setUp() throws Exception {
jdbcUrl = "jdbc:fake:dragoon_v25masterdb";
@@ -93,14 +93,14 @@ public class Case0 extends TestCase {
}
System.out.println();
}
public void test_2() throws Exception {
BoneCPDataSource dataSource = new BoneCPDataSource();
// dataSource.(10);
// dataSource.setMaxActive(50);
dataSource.setMinConnectionsPerPartition(minPoolSize);
dataSource.setMaxConnectionsPerPartition(maxPoolSize);
dataSource.setDriverClass(driverClass);
dataSource.setJdbcUrl(jdbcUrl);
// dataSource.setPoolPreparedStatements(true);
@@ -109,7 +109,7 @@ public class Case0 extends TestCase {
dataSource.setPassword(password);
dataSource.setConnectionTestStatement("SELECT 1");
dataSource.setPartitionCount(1);
for (int i = 0; i < LOOP_COUNT; ++i) {
p0(dataSource, "boneCP");
}