📄 connectionwrapper.java
字号:
/* Copyright 2002-2007 MySQL AB, 2008 Sun Microsystems This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. There are special exceptions to the terms and conditions of the GPL as it is applied to this software. View the full text of the exception in file EXCEPTIONS-CONNECTOR-J in the directory of this software distribution. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package com.mysql.jdbc.jdbc2.optional;import java.lang.reflect.Constructor;import java.sql.SQLException;import java.sql.Savepoint;import java.sql.Statement;import java.util.Map;import java.util.TimeZone;import com.mysql.jdbc.Connection;import com.mysql.jdbc.Extension;import com.mysql.jdbc.MysqlErrorNumbers;import com.mysql.jdbc.SQLError;import com.mysql.jdbc.Util;import com.mysql.jdbc.log.Log;/** * This class serves as a wrapper for the org.gjt.mm.mysql.jdbc2.Connection * class. It is returned to the application server which may wrap it again and * then return it to the application client in response to * dataSource.getConnection(). * * <p> * All method invocations are forwarded to org.gjt.mm.mysql.jdbc2.Connection * unless the close method was previously called, in which case a sqlException * is thrown. The close method performs a 'logical close' on the connection. * </p> * * <p> * All sqlExceptions thrown by the physical connection are intercepted and sent * to connectionEvent listeners before being thrown to client. * </p> * * @author Todd Wolff todd.wolff_at_prodigy.net * * @see org.gjt.mm.mysql.jdbc2.Connection * @see org.gjt.mm.mysql.jdbc2.optional.MysqlPooledConnection */public class ConnectionWrapper extends WrapperBase implements Connection { protected Connection mc = null; private MysqlPooledConnection mpc = null; private String invalidHandleStr = "Logical handle no longer valid"; private boolean closed; private boolean isForXa; private static final Constructor JDBC_4_CONNECTION_WRAPPER_CTOR; static { if (Util.isJdbc4()) { try { JDBC_4_CONNECTION_WRAPPER_CTOR = Class.forName( "com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper") .getConstructor( new Class[] { MysqlPooledConnection.class, Connection.class, Boolean.TYPE }); } catch (SecurityException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } else { JDBC_4_CONNECTION_WRAPPER_CTOR = null; } } protected static ConnectionWrapper getInstance( MysqlPooledConnection mysqlPooledConnection, Connection mysqlConnection, boolean forXa) throws SQLException { if (!Util.isJdbc4()) { return new ConnectionWrapper(mysqlPooledConnection, mysqlConnection, forXa); } return (ConnectionWrapper) Util.handleNewInstance( JDBC_4_CONNECTION_WRAPPER_CTOR, new Object[] { mysqlPooledConnection, mysqlConnection, Boolean.valueOf(forXa) }); } /** * Construct a new LogicalHandle and set instance variables * * @param mysqlPooledConnection * reference to object that instantiated this object * @param mysqlConnection * physical connection to db * * @throws SQLException * if an error occurs. */ public ConnectionWrapper(MysqlPooledConnection mysqlPooledConnection, Connection mysqlConnection, boolean forXa) throws SQLException { this.mpc = mysqlPooledConnection; this.mc = mysqlConnection; this.closed = false; this.pooledConnection = this.mpc; this.isForXa = forXa; if (this.isForXa) { setInGlobalTx(false); } } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @see java.sql.Connection#setAutoCommit */ public void setAutoCommit(boolean autoCommit) throws SQLException { checkClosed(); if (autoCommit && isInGlobalTx()) { throw SQLError.createSQLException( "Can't set autocommit to 'true' on an XAConnection", SQLError.SQL_STATE_INVALID_TRANSACTION_TERMINATION, MysqlErrorNumbers.ER_XA_RMERR); } try { this.mc.setAutoCommit(autoCommit); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @see java.sql.Connection#getAutoCommit() */ public boolean getAutoCommit() throws SQLException { checkClosed(); try { return this.mc.getAutoCommit(); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } return false; // we don't reach this code, compiler can't tell } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @see java.sql.Connection#setCatalog() */ public void setCatalog(String catalog) throws SQLException { checkClosed(); try { this.mc.setCatalog(catalog); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @return the current catalog * * @throws SQLException * if an error occurs */ public String getCatalog() throws SQLException { checkClosed(); try { return this.mc.getCatalog(); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } return null; // we don't reach this code, compiler can't tell } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @see java.sql.Connection#isClosed() */ public boolean isClosed() throws SQLException { return (this.closed || this.mc.isClosed()); } public boolean isMasterConnection() { return this.mc.isMasterConnection(); } /** * @see Connection#setHoldability(int) */ public void setHoldability(int arg0) throws SQLException { checkClosed(); try { this.mc.setHoldability(arg0); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } } /** * @see Connection#getHoldability() */ public int getHoldability() throws SQLException { checkClosed(); try { return this.mc.getHoldability(); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } return Statement.CLOSE_CURRENT_RESULT; // we don't reach this code, // compiler can't tell } /** * Allows clients to determine how long this connection has been idle. * * @return how long the connection has been idle. */ public long getIdleFor() { return this.mc.getIdleFor(); } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @return a metadata instance * * @throws SQLException * if an error occurs */ public java.sql.DatabaseMetaData getMetaData() throws SQLException { checkClosed(); try { return this.mc.getMetaData(); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } return null; // we don't reach this code, compiler can't tell } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @see java.sql.Connection#setReadOnly() */ public void setReadOnly(boolean readOnly) throws SQLException { checkClosed(); try { this.mc.setReadOnly(readOnly); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @see java.sql.Connection#isReadOnly() */ public boolean isReadOnly() throws SQLException { checkClosed(); try { return this.mc.isReadOnly(); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } return false; // we don't reach this code, compiler can't tell } /** * @see Connection#setSavepoint() */ public java.sql.Savepoint setSavepoint() throws SQLException { checkClosed(); if (isInGlobalTx()) { throw SQLError.createSQLException( "Can't set autocommit to 'true' on an XAConnection", SQLError.SQL_STATE_INVALID_TRANSACTION_TERMINATION, MysqlErrorNumbers.ER_XA_RMERR); } try { return this.mc.setSavepoint(); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } return null; // we don't reach this code, compiler can't tell } /** * @see Connection#setSavepoint(String) */ public java.sql.Savepoint setSavepoint(String arg0) throws SQLException { checkClosed(); if (isInGlobalTx()) { throw SQLError.createSQLException( "Can't set autocommit to 'true' on an XAConnection", SQLError.SQL_STATE_INVALID_TRANSACTION_TERMINATION, MysqlErrorNumbers.ER_XA_RMERR); } try { return this.mc.setSavepoint(arg0); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } return null; // we don't reach this code, compiler can't tell } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @see java.sql.Connection#setTransactionIsolation() */ public void setTransactionIsolation(int level) throws SQLException { checkClosed(); try { this.mc.setTransactionIsolation(level); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @see java.sql.Connection#getTransactionIsolation() */ public int getTransactionIsolation() throws SQLException { checkClosed(); try { return this.mc.getTransactionIsolation(); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } return TRANSACTION_REPEATABLE_READ; // we don't reach this code, // compiler can't tell } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @see java.sql.Connection#setTypeMap() */ public void setTypeMap(java.util.Map map) throws SQLException { checkClosed(); try { this.mc.setTypeMap(map); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @see java.sql.Connection#getTypeMap() */ public java.util.Map getTypeMap() throws SQLException { checkClosed(); try { return this.mc.getTypeMap(); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } return null; // we don't reach this code, compiler can't tell } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @see java.sql.Connection#getWarnings */ public java.sql.SQLWarning getWarnings() throws SQLException { checkClosed(); try { return this.mc.getWarnings(); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } return null; // we don't reach this code, compiler can't tell } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @throws SQLException * if an error occurs */ public void clearWarnings() throws SQLException { checkClosed(); try { this.mc.clearWarnings(); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } } /** * The physical connection is not actually closed. the physical connection * is closed when the application server calls * mysqlPooledConnection.close(). this object is de-referenced by the pooled * connection each time mysqlPooledConnection.getConnection() is called by * app server. * * @throws SQLException * if an error occurs */ public void close() throws SQLException { close(true); } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @throws SQLException * if an error occurs */ public void commit() throws SQLException { checkClosed(); if (isInGlobalTx()) { throw SQLError .createSQLException( "Can't call commit() on an XAConnection associated with a global transaction", SQLError.SQL_STATE_INVALID_TRANSACTION_TERMINATION, MysqlErrorNumbers.ER_XA_RMERR); } try { this.mc.commit(); } catch (SQLException sqlException) { checkAndFireConnectionError(sqlException); } } /** * Passes call to method on physical connection instance. Notifies listeners * of any caught exceptions before re-throwing to client. * * @see java.sql.Connection#createStatement() */ public java.sql.Statement createStatement() throws SQLException { checkClosed(); try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -