📄 connection.java
字号:
} else if (host.indexOf(",") != -1) {
// multiple hosts separated by commas (failover)
hostList = StringUtils.split(host, ",", true);
} else {
this.host = host;
hostList = new ArrayList();
hostList.add(this.host);
}
hostListSize = hostList.size();
this.port = port;
if (database == null) {
throw new SQLException("Malformed URL '" + url + "'.",
SQLError.SQL_STATE_GENERAL_ERROR);
}
this.database = database;
this.myURL = url;
this.myDriver = d;
this.user = info.getProperty("user");
this.password = info.getProperty("password");
if ((this.user == null) || this.user.equals("")) {
this.user = "nobody";
}
if (this.password == null) {
this.password = "";
}
this.props = info;
initializeDriverProperties(info);
if (Driver.DEBUG) {
System.out.println("Connect: " + this.user + " to " + this.database);
}
try {
createNewIO(false);
this.dbmd = new DatabaseMetaData(this, this.database);
} catch (java.sql.SQLException ex) {
cleanup(ex);
// don't clobber SQL exceptions
throw ex;
} catch (Exception ex) {
cleanup(ex);
StringBuffer mesg = new StringBuffer();
if (!useParanoidErrorMessages()) {
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());
throw new java.sql.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 autoCommit - true enables auto-commit; false disables it
*
* @exception java.sql.SQLException if a database access error occurs
* @throws SQLException DOCUMENT ME!
*/
public void setAutoCommit(boolean autoCommit) throws java.sql.SQLException {
if (Driver.TRACE) {
Object[] args = { new Boolean(autoCommit) };
Debug.methodCall(this, "setAutoCommit", args);
}
checkClosed();
if (this.transactionsSupported) {
// 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 = autoCommit;
//
// This is to catch the 'edge' case of
// autoCommit going from true -> false
//
if ((this.highAvailability || this.failedOver) && !this.autoCommit) {
pingAndReconnect(true);
}
String sql = "SET autocommit=" + (autoCommit ? "1" : "0");
execSQL(sql, -1, this.database);
} else {
if ((autoCommit == false) && (this.relaxAutoCommit == false)) {
throw new SQLException("MySQL Versions Older than 3.23.15 "
+ "do not support transactions",
SQLError.SQL_STATE_DRIVER_NOT_CAPABLE);
} else {
this.autoCommit = autoCommit;
}
}
return;
}
/**
* gets the current auto-commit state
*
* @return Current state of the auto-commit mode
*
* @exception java.sql.SQLException (why?)
*
* @see setAutoCommit
*/
public boolean getAutoCommit() throws java.sql.SQLException {
if (Driver.TRACE) {
Object[] args = new Object[0];
Debug.methodCall(this, "getAutoCommit", args);
Debug.returnValue(this, "getAutoCommit",
new Boolean(this.autoCommit));
}
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 java.sql.SQLException if a database access error occurs
*/
public void setCatalog(String catalog) throws java.sql.SQLException {
if (Driver.TRACE) {
Object[] args = { catalog };
Debug.methodCall(this, "setCatalog", args);
}
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(query.toString(), -1, catalog);
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 java.sql.SQLException if a database access error occurs
*/
public String getCatalog() throws java.sql.SQLException {
if (Driver.TRACE) {
Object[] args = new Object[0];
Debug.methodCall(this, "getCatalog", args);
Debug.returnValue(this, "getCatalog", this.database);
}
return this.database;
}
/**
* Returns whether we clobber streaming results on new queries, or issue an
* error?
*
* @return true if we should implicitly close streaming result sets upon
* receiving a new query
*/
public boolean getClobberStreamingResults() {
return this.clobberStreamingResults;
}
/**
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
public boolean isClosed() {
if (Driver.TRACE) {
Object[] args = new Object[0];
Debug.methodCall(this, "isClosed", args);
Debug.returnValue(this, "isClosed", new Boolean(this.isClosed));
}
return this.isClosed;
}
/**
* Returns the character encoding for this Connection
*
* @return the character encoding for this connection.
*/
public String getEncoding() {
return this.encoding;
}
/**
* @see Connection#setHoldability(int)
*/
public void setHoldability(int arg0) throws SQLException {
// do nothing
}
/**
* @see Connection#getHoldability()
*/
public int getHoldability() throws SQLException {
return 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;
} else {
long now = System.currentTimeMillis();
long idleTime = now - this.lastQueryFinishedTime;
return idleTime;
}
}
/**
* Should we tell MySQL that we're an interactive client
*
* @return true if isInteractiveClient was set to true.
*/
public boolean isInteractiveClient() {
return isInteractiveClient;
}
/**
* 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 java.sql.SQLException if a database access error occurs
*/
public java.sql.DatabaseMetaData getMetaData() throws java.sql.SQLException {
checkClosed();
return new DatabaseMetaData(this, this.database);
}
/**
* DOCUMENT ME!
*
* @return
*/
public String getNegativeInfinityRep() {
return negativeInfinityRep;
}
/**
* DOCUMENT ME!
*
* @return
*/
public boolean isNegativeInfinityRepIsClipped() {
return negativeInfinityRepIsClipped;
}
/**
* DOCUMENT ME!
*
* @return
*/
public String getNotANumberRep() {
return notANumberRep;
}
/**
* DOCUMENT ME!
*
* @return
*/
public boolean isNotANumberRepIsClipped() {
return notANumberRepIsClipped;
}
/**
* DOCUMENT ME!
*
* @return
*/
public String getPositiveInfinityRep() {
return positiveInfinityRep;
}
/**
* DOCUMENT ME!
*
* @return
*/
public boolean isPositiveInfinityRepIsClipped() {
return positiveInfinityRepIsClipped;
}
/**
* Should the driver do profiling?
*
* @param flag set to true to enable profiling.
*
* @throws SQLException if the connection is closed.
*/
public void setProfileSql(boolean flag) throws SQLException {
// For re-connection
this.props.setProperty("profileSql", String.valueOf(flag));
getIO().setProfileSql(flag);
}
/**
* 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 readOnly - true enables read-only mode; false disables it
*
* @exception java.sql.SQLException if a database access error occurs
*/
public void setReadOnly(boolean readOnly) throws java.sql.SQLException {
if (Driver.TRACE) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -