📄 xadatasourceimpl.java
字号:
// XXX We should encrypt this part so as not to hold the
// password in memory
if (password == null)
return user;
else
return user + ":" + password;
}
/**
* Creates a new underlying connection. Used by XA connection
* that lost it's underlying connection when joining a
* transaction and is now asked to produce a new connection.
*
* @param userName the userName
* @param password the password
* @return An open connection ready for use
* @throws SQLException An error occured trying to open
* a connection
*/
Connection newConnection(String userName, String password)
throws SQLException {
Connection connection;
synchronized (_pool) {
// Check in the pool first.
if (!_pool.isEmpty()) {
String account = getAccount(userName, password);
ConnectionEntry entry;
for (Iterator i = _pool.iterator(); i.hasNext();) {
entry = (ConnectionEntry) i.next();
if (entry._account.equals(account)) {
i.remove();
//entry._connection.setTransactionIsolation(_isolationLevel);
return entry._connection;
}
}
}
}
connection = getConnection(userName, password);
//connection.setTransactionIsolation(_isolationLevel);
return connection;
}
/**
* XXX Not fully implemented yet and no code to really
* test it.
*/
Xid[] getTxRecover() {
Vector list;
Enumeration enum;
TxConnection txConn;
list = new Vector();
enum = _txConnections.elements();
while (enum.hasMoreElements()) {
txConn = (TxConnection) enum.nextElement();
if (txConn.conn != null && txConn.prepared)
list.add(txConn.xid);
}
return (Xid[]) list.toArray();
}
/**
* Returns the transaction isolation level to use with all newly
* created transactions, or {@link Connection#TRANSACTION_NONE}
* if using the driver's default isolation level.
*/
public int isolationLevel() {
return Connection.TRANSACTION_NONE;
}
public void run() {
Enumeration enum;
int reduce;
long timeout;
TxConnection txConn;
while (true) {
// Go to sleep for the duration of a transaction
// timeout. This mean transactions will timeout on average
// at _txTimeout * 1.5.
try {
Thread.sleep(_txTimeout * 1000);
}
catch (InterruptedException except) {
}
try {
// Check to see if there are any pooled connections
// we can release. We release 10% of the pooled
// connections each time, so in a heavy loaded
// environment we don't get to release that many, but
// as load goes down we do. These are not actually
// pooled connections, but connections that happen to
// get in and out of a transaction, not that many.
int size = _pool.size();
reduce = size - (size / 10) - 1;
if (reduce >= 0 && size > reduce) {
if (getLogWriter() != null)
getLogWriter().println(
"DataSource "
+ toString()
+ ": Reducing internal connection pool size from "
+ size
+ " to "
+ reduce);
Iterator iterator = _pool.iterator();
do {
try {
((ConnectionEntry) iterator.next())._connection.close();
}
catch (SQLException except) {
}
iterator.remove();
}
while (--size > reduce);
}
}
catch (Exception except) {
}
// Look for all connections inside a transaction that
// should have timed out by now.
timeout = System.currentTimeMillis();
enum = _txConnections.elements();
while (enum.hasMoreElements()) {
txConn = (TxConnection) enum.nextElement();
// If the transaction timed out, we roll it back and
// invalidate it, but do not remove it from the transaction
// list yet. We wait for the next iteration, minimizing the
// chance of a NOTA exception.
if (txConn.conn == null) {
_txConnections.remove(txConn.xid);
// Chose not to use an iterator so we must
// re-enumerate the list after removing
// an element from it.
enum = _txConnections.elements();
}
else
if (txConn.timeout < timeout) {
try {
Connection underlying;
synchronized (txConn) {
if (txConn.conn == null)
continue;
if (getLogWriter() != null)
getLogWriter().println(
"DataSource "
+ toString()
+ ": Transaction timed out and being aborted: "
+ txConn.xid);
// Remove the connection from the transaction
// association. XAConnection will now have
// no underlying connection and attempt to
// create a new one.
underlying = txConn.conn;
txConn.conn = null;
txConn.timedOut = true;
// Rollback the underlying connection to
// abort the transaction and release the
// underlying connection to the pool.
try {
underlying.rollback();
releaseConnection(underlying, txConn.userName, txConn.password);
}
catch (SQLException except) {
if (getLogWriter() != null)
getLogWriter().println(
"DataSource "
+ toString()
+ ": Error aborting timed out transaction: "
+ except);
try {
underlying.close();
}
catch (SQLException e2) {
}
}
}
}
catch (Exception except) {
}
}
}
}
}
public void debug(PrintWriter writer) {
Enumeration enum;
TxConnection txConn;
StringBuffer buffer;
writer.println("Debug info for XADataSource:");
enum = _txConnections.elements();
if (!enum.hasMoreElements())
writer.println("Empty");
while (enum.hasMoreElements()) {
buffer = new StringBuffer();
txConn = (TxConnection) enum.nextElement();
buffer.append("TxConnection ");
if (txConn.xid != null)
buffer.append(txConn.xid);
if (txConn.conn != null)
buffer.append(' ').append(txConn.conn);
buffer.append(" count: ").append(txConn.count);
if (txConn.prepared)
buffer.append(" prepared");
if (txConn.timedOut)
buffer.append(" timed-out");
if (txConn.readOnly)
buffer.append(" read-only");
writer.println(buffer.toString());
}
Iterator iterator = _pool.iterator();
while (iterator.hasNext())
writer.println("Pooled underlying: " + iterator.next().toString());
}
/**
* Object to hold a connection and its user name
* and password. This object is immutable.
*/
private static class ConnectionEntry {
/**
* The account
*/
private final String _account;
/**
* The connection
*/
private final Connection _connection;
/**
* Create the ConnectionEntry with the specified
* arguments
*
* @param connection the connection
* @param userName the userName
* @param password the password
*/
private ConnectionEntry(Connection connection, String account) {
_connection = connection;
_account = account;
}
/**
* Return the printed representation
* of the connection entry.
*
* @return the printed representation
* of the connection entry.
*/
public String toString() {
return _connection.toString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -