mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
支持XA
git-svn-id: http://code.alibabatech.com/svn/druid/trunk@27 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
@@ -19,21 +19,170 @@ import java.sql.SQLException;
|
||||
|
||||
import javax.sql.XAConnection;
|
||||
import javax.sql.XADataSource;
|
||||
import javax.transaction.TransactionManager;
|
||||
|
||||
import org.apache.commons.dbcp.AbandonedConfig;
|
||||
import org.apache.commons.dbcp.PoolableConnectionFactory;
|
||||
import org.apache.commons.dbcp.PoolingDataSource;
|
||||
import org.apache.commons.dbcp.managed.ManagedDataSource;
|
||||
import org.apache.commons.dbcp.managed.PoolableManagedConnectionFactory;
|
||||
import org.apache.commons.pool.KeyedObjectPoolFactory;
|
||||
|
||||
import com.alibaba.druid.pool.ConnectionFactory;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class DruidXADataSource extends DruidDataSource implements XADataSource {
|
||||
public class DruidXADataSource extends DruidDataSource {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public XAConnection getXAConnection() throws SQLException {
|
||||
return null;
|
||||
/** Transaction Registry */
|
||||
private TransactionRegistry transactionRegistry;
|
||||
/** Transaction Manager */
|
||||
private transient TransactionManager transactionManager;
|
||||
/** XA datasource class name */
|
||||
private String xaDataSource;
|
||||
/** XA datasource instance */
|
||||
private XADataSource xaDataSourceInstance;
|
||||
|
||||
/**
|
||||
* Gets the XADataSource instance used by the XAConnectionFactory.
|
||||
*
|
||||
* @return the XADataSource
|
||||
*/
|
||||
public synchronized XADataSource getXaDataSourceInstance() {
|
||||
return xaDataSourceInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public XAConnection getXAConnection(String user, String password) throws SQLException {
|
||||
return null;
|
||||
/**
|
||||
* <p>
|
||||
* Sets the XADataSource instance used by the XAConnectionFactory.
|
||||
* </p>
|
||||
* <p>
|
||||
* Note: this method currently has no effect once the pool has been initialized. The pool is initialized the first
|
||||
* time one of the following methods is invoked: <code>getConnection, setLogwriter,
|
||||
* setLoginTimeout, getLoginTimeout, getLogWriter.</code>
|
||||
* </p>
|
||||
*
|
||||
* @param xaDataSourceInstance XADataSource instance
|
||||
*/
|
||||
public synchronized void setXaDataSourceInstance(XADataSource xaDataSourceInstance) {
|
||||
this.xaDataSourceInstance = xaDataSourceInstance;
|
||||
xaDataSource = xaDataSourceInstance == null ? null : xaDataSourceInstance.getClass().getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the required transaction manager property.
|
||||
*
|
||||
* @return the transaction manager used to enlist connections
|
||||
*/
|
||||
public TransactionManager getTransactionManager() {
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the transaction registry.
|
||||
*
|
||||
* @return the transaction registry associating XAResources with managed connections
|
||||
*/
|
||||
protected synchronized TransactionRegistry getTransactionRegistry() {
|
||||
return transactionRegistry;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the required transaction manager property.
|
||||
*
|
||||
* @param transactionManager the transaction manager used to enlist connections
|
||||
*/
|
||||
public void setTransactionManager(TransactionManager transactionManager) {
|
||||
this.transactionManager = transactionManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the optional XADataSource class name.
|
||||
*
|
||||
* @return the optional XADataSource class name
|
||||
*/
|
||||
public synchronized String getXADataSource() {
|
||||
return xaDataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the optional XADataSource class name.
|
||||
*
|
||||
* @param xaDataSource the optional XADataSource class name
|
||||
*/
|
||||
public synchronized void setXADataSource(String xaDataSource) {
|
||||
this.xaDataSource = xaDataSource;
|
||||
}
|
||||
|
||||
protected ConnectionFactory createConnectionFactory() throws SQLException {
|
||||
if (transactionManager == null) {
|
||||
throw new SQLException("Transaction manager must be set before a connection can be created");
|
||||
}
|
||||
|
||||
// If xa data source is not specified a DriverConnectionFactory is created and wrapped with a
|
||||
// LocalXAConnectionFactory
|
||||
if (xaDataSource == null) {
|
||||
ConnectionFactory connectionFactory = super.createConnectionFactory();
|
||||
XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory(getTransactionManager(),
|
||||
connectionFactory);
|
||||
transactionRegistry = xaConnectionFactory.getTransactionRegistry();
|
||||
return xaConnectionFactory;
|
||||
}
|
||||
|
||||
// Create the XADataSource instance using the configured class name if it has not been set
|
||||
if (xaDataSourceInstance == null) {
|
||||
Class xaDataSourceClass = null;
|
||||
try {
|
||||
xaDataSourceClass = Class.forName(xaDataSource);
|
||||
} catch (Throwable t) {
|
||||
String message = "Cannot load XA data source class '" + xaDataSource + "'";
|
||||
throw (SQLException) new SQLException(message).initCause(t);
|
||||
}
|
||||
|
||||
try {
|
||||
xaDataSourceInstance = (XADataSource) xaDataSourceClass.newInstance();
|
||||
} catch (Throwable t) {
|
||||
String message = "Cannot create XA data source of class '" + xaDataSource + "'";
|
||||
throw (SQLException) new SQLException(message).initCause(t);
|
||||
}
|
||||
}
|
||||
|
||||
// finally, create the XAConectionFactory using the XA data source
|
||||
XAConnectionFactory xaConnectionFactory = new DataSourceXAConnectionFactory(getTransactionManager(),
|
||||
xaDataSourceInstance, username,
|
||||
password);
|
||||
transactionRegistry = xaConnectionFactory.getTransactionRegistry();
|
||||
return xaConnectionFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the PoolableConnectionFactory and attaches it to the connection pool.
|
||||
*
|
||||
* @param driverConnectionFactory JDBC connection factory created by {@link #createConnectionFactory()}
|
||||
* @param statementPoolFactory statement pool factory (null if statement pooling is turned off)
|
||||
* @param abandonedConfig abandoned connection tracking configuration (null if no tracking)
|
||||
* @throws SQLException if an error occurs creating the PoolableConnectionFactory
|
||||
*/
|
||||
protected void createPoolableConnectionFactory(ConnectionFactory driverConnectionFactory,
|
||||
KeyedObjectPoolFactory statementPoolFactory,
|
||||
AbandonedConfig abandonedConfig) throws SQLException {
|
||||
// PoolableConnectionFactory connectionFactory = null;
|
||||
// try {
|
||||
// connectionFactory = new PoolableManagedConnectionFactory((XAConnectionFactory) driverConnectionFactory,
|
||||
// connectionPool, statementPoolFactory,
|
||||
// validationQuery, validationQueryTimeout,
|
||||
// connectionInitSqls, defaultReadOnly,
|
||||
// defaultAutoCommit, defaultTransactionIsolation,
|
||||
// defaultCatalog, abandonedConfig);
|
||||
// validateConnectionFactory(connectionFactory);
|
||||
// } catch (RuntimeException e) {
|
||||
// throw e;
|
||||
// } catch (Exception e) {
|
||||
// throw (SQLException) new SQLException("Cannot create PoolableConnectionFactory (" + e.getMessage() +
|
||||
// ")").initCause(e);
|
||||
// }
|
||||
throw new SQLException("TODO");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user