📄 connection.java
字号:
this.isClientTzUTC = true; } else { this.isClientTzUTC = false; } this.openStatements = new HashMap(); this.serverVariables = new HashMap(); this.hostList = new ArrayList(); if (hostToConnectTo == null) { this.host = "localhost"; this.hostList.add(this.host); } else if (hostToConnectTo.indexOf(",") != -1) { // multiple hosts separated by commas (failover) StringTokenizer hostTokenizer = new StringTokenizer(hostToConnectTo, ",", false); while (hostTokenizer.hasMoreTokens()) { this.hostList.add(hostTokenizer.nextToken().trim()); } } else { this.host = hostToConnectTo; this.hostList.add(this.host); } this.hostListSize = this.hostList.size(); this.port = portToConnectTo; if (databaseToConnectTo == null) { databaseToConnectTo = ""; } this.database = databaseToConnectTo; this.myURL = url; this.user = info.getProperty(NonRegisteringDriver.USER_PROPERTY_KEY); this.password = info.getProperty(NonRegisteringDriver.PASSWORD_PROPERTY_KEY); if ((this.user == null) || this.user.equals("")) { this.user = ""; } if (this.password == null) { this.password = ""; } this.props = info; initializeDriverProperties(info); try { createNewIO(false); this.dbmd = new DatabaseMetaData(this, this.database); } catch (SQLException ex) { cleanup(new Throwable(), ex); // don't clobber SQL exceptions throw ex; } catch (Exception ex) { cleanup(new Throwable(), ex); StringBuffer mesg = new StringBuffer(); if (getParanoid()) { mesg.append("Cannot connect to MySQL server on "); mesg.append(this.host); mesg.append(":"); mesg.append(this.port); mesg.append(".\n\n"); mesg.append("Make sure that there is a MySQL server "); mesg.append("running on the machine/port you are trying "); mesg.append( "to connect to and that the machine this software is " + "running on "); mesg.append("is able to connect to this host/port " + "(i.e. not firewalled). "); mesg.append( "Also make sure that the server has not been started " + "with the --skip-networking "); mesg.append("flag.\n\n"); } else { mesg.append("Unable to connect to database."); } mesg.append("Underlying exception: \n\n"); mesg.append(ex.getClass().getName()); if (!getParanoid()) { mesg.append(Util.stackTraceToString(ex)); } throw new SQLException(mesg.toString(), SQLError.SQL_STATE_COMMUNICATION_LINK_FAILURE); } } /** * If a connection is in auto-commit mode, than all its SQL statements will * be executed and committed as individual transactions. Otherwise, its * SQL statements are grouped into transactions that are terminated by * either commit() or rollback(). By default, new connections are in * auto- commit mode. The commit occurs when the statement completes or * the next execute occurs, whichever comes first. In the case of * statements returning a ResultSet, the statement completes when the last * row of the ResultSet has been retrieved or the ResultSet has been * closed. In advanced cases, a single statement may return multiple * results as well as output parameter values. Here the commit occurs * when all results and output param values have been retrieved. * * <p> * <b>Note:</b> MySQL does not support transactions, so this method is a * no-op. * </p> * * @param autoCommitFlag - true enables auto-commit; false disables it * * @exception SQLException if a database access error occurs */ public void setAutoCommit(boolean autoCommitFlag) throws SQLException { checkClosed(); if (getAutoReconnectForPools()) { setHighAvailability(true); } try { if (this.transactionsSupported) { boolean needsSetOnServer = true; if (this.getUseLocalSessionState() && this.autoCommit == autoCommitFlag) { needsSetOnServer = false; } else if (!this.getHighAvailability()) { needsSetOnServer = this.getIO().isSetNeededForAutoCommitMode(autoCommitFlag); } // this internal value must be set first as failover depends on it // being set to true to fail over (which is done by most // app servers and connection pools at the end of // a transaction), and the driver issues an implicit set // based on this value when it (re)-connects to a server // so the value holds across connections this.autoCommit = autoCommitFlag; if (needsSetOnServer) { execSQL(null, autoCommitFlag ? "SET autocommit=1" : "SET autocommit=0", -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.database, true, Statement.USES_VARIABLES_FALSE); } } else { if ((autoCommitFlag == false) && !getRelaxAutoCommit()) { throw new SQLException("MySQL Versions Older than 3.23.15 " + "do not support transactions", SQLError.SQL_STATE_CONNECTION_NOT_OPEN); } this.autoCommit = autoCommitFlag; } } finally { if (this.getAutoReconnectForPools()) { setHighAvailability(false); } } return; } /** * Gets the current auto-commit state * * @return Current state of auto-commit * * @exception SQLException if an error occurs * * @see setAutoCommit */ public boolean getAutoCommit() throws SQLException { return this.autoCommit; } /** * A sub-space of this Connection's database may be selected by setting a * catalog name. If the driver does not support catalogs, it will * silently ignore this request * * <p> * <b>Note:</b> MySQL's notion of catalogs are individual databases. * </p> * * @param catalog the database for this connection to use * * @throws SQLException if a database access error occurs */ public void setCatalog(String catalog) throws SQLException { checkClosed(); String quotedId = this.dbmd.getIdentifierQuoteString(); if ((quotedId == null) || quotedId.equals(" ")) { quotedId = ""; } StringBuffer query = new StringBuffer("USE "); query.append(quotedId); query.append(catalog); query.append(quotedId); execSQL(null, query.toString(), -1, null, java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, false, false, this.database, true, Statement.USES_VARIABLES_FALSE); this.database = catalog; } /** * Return the connections current catalog name, or null if no catalog name * is set, or we dont support catalogs. * * <p> * <b>Note:</b> MySQL's notion of catalogs are individual databases. * </p> * * @return the current catalog name or null * * @exception SQLException if a database access error occurs */ public String getCatalog() throws SQLException { return this.database; } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public boolean isClosed() { return this.isClosed; } /** * @see Connection#setHoldability(int) */ public void setHoldability(int arg0) throws SQLException { // do nothing } /** * @see Connection#getHoldability() */ public int getHoldability() throws SQLException { return java.sql.ResultSet.CLOSE_CURSORS_AT_COMMIT; } /** * NOT JDBC-Compliant, but clients can use this method to determine how * long this connection has been idle. This time (reported in * milliseconds) is updated once a query has completed. * * @return number of ms that this connection has been idle, 0 if the driver * is busy retrieving results. */ public long getIdleFor() { if (this.lastQueryFinishedTime == 0) { return 0; } long now = System.currentTimeMillis(); long idleTime = now - this.lastQueryFinishedTime; return idleTime; } /** * Returns the log mechanism that should be used to log information * from/for this Connection. * * @return the Log instance to use for logging messages. * * @throws SQLException if an error occurs */ public Log getLog() throws SQLException { return this.log; } /** * A connection's database is able to provide information describing its * tables, its supported SQL grammar, its stored procedures, the * capabilities of this connection, etc. This information is made * available through a DatabaseMetaData object. * * @return a DatabaseMetaData object for this connection * * @exception SQLException if a database access error occurs */ public java.sql.DatabaseMetaData getMetaData() throws SQLException { checkClosed(); return new DatabaseMetaData(this, this.database); } /** * You can put a connection in read-only mode as a hint to enable database * optimizations <B>Note:</B> setReadOnly cannot be called while in the * middle of a transaction * * @param readOnlyFlag - true enables read-only mode; false disables it * * @exception SQLException if a database access error occurs */ public void setReadOnly(boolean readOnlyFlag) throws SQLException { checkClosed(); this.readOnly = readOnlyFlag; } /** * Tests to see if the connection is in Read Only Mode. Note that we * cannot really put the database in read only mode, but we pretend we can * by returning the value of the readOnly flag * * @return true if the connection is read only * * @exception SQLException if a database access error occurs */ public boolean isReadOnly() throws SQLException { return this.readOnly; } /** * @see Connection#setSavepoint() */ public java.sql.Savepoint setSavepoint() throws SQLException { MysqlSavepoint savepoint = new MysqlSavepoint(); setSavepoint(savepoint); return savepoint; } /** * @see Connection#setSavepoint(String) */ public java.sql.Savepoint setSavepoint(String name) throws SQLException { MysqlSavepoint savepoint = new MysqlSavepoint(name); setSavepoint(savepoint); return savepoint; } /** * DOCUMENT ME! * * @return DOCUMENT ME! */ public TimeZone getServerTimezoneTZ() { return this.serverTimezoneTZ; } /** * DOCUMENT ME! * * @param level DOCUMENT ME! * * @throws SQLException DOCUMENT ME! */ public void setTransactionIsolation(int level) throws SQLException { checkClosed(); if (this.hasIsolationLevels) { String sql = null; boolean shouldSendSet = false; if (getAlwaysSendSetIsolation()) { shouldSendSet = true; } else { if (level != this.isolationLevel) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -