📄 connectionmanager.java
字号:
while ((len = bodyReader.read(buf)) >= 0) {
out.write(buf, 0, len);
}
value = out.toString();
out.close();
}
catch (Exception e) {
e.printStackTrace();
throw new SQLException("Failed to load text field");
}
finally {
try { bodyReader.close(); }
catch(Exception e) { }
}
return value;
}
else {
return rs.getString(columnIndex);
}
}
/**
* Sets a large text column in a result set, automatically performing
* streaming if the JDBC driver requires it. This is necessary because
* different JDBC drivers have different capabilities and methods for
* setting large text values.
*
* @param pstmt the PreparedStatement to set the text field in.
* @param parameterIndex the index corresponding to the text field.
* @param value the String to set.
*/
public static void setLargeTextField(PreparedStatement pstmt,
int parameterIndex, String value) throws SQLException
{
if (streamLargeText) {
Reader bodyReader = null;
try {
bodyReader = new StringReader(value);
pstmt.setCharacterStream(parameterIndex, bodyReader, value.length());
}
catch (Exception e) {
e.printStackTrace();
throw new SQLException("Failed to set text field.");
}
// Leave bodyReader open so that the db can read from it. It *should*
// be garbage collected after it's done without needing to call close.
}
else {
pstmt.setString(parameterIndex, value);
}
}
/**
* Sets the max number of rows that should be returned from executing a
* statement. The operation is automatically bypassed if Jive knows that the
* the JDBC driver or database doesn't support it.
*
* @param stmt the Statement to set the max number of rows for.
* @param maxRows the max number of rows to return.
*/
public static void setMaxRows(Statement stmt, int maxRows)
throws SQLException
{
if (supportsMaxRows) {
try {
stmt.setMaxRows(maxRows);
}
catch (Throwable t) {
// Ignore. Exception may happen if the driver doesn't support
// this operation and we didn't set meta-data correctly.
// However, it is a good idea to update the meta-data so that
// we don't have to incur the cost of catching an exception
// each time.
}
}
}
/**
* Sets the number of rows that the JDBC driver should buffer at a time.
* The operation is automatically bypassed if Jive knows that the
* the JDBC driver or database doesn't support it.
*
* @param rs the ResultSet to set the fetch size for.
* @param fetchSize the fetchSize.
*/
public static void setFetchSize(ResultSet rs, int fetchSize)
throws SQLException
{
if (supportsFetchSize) {
try {
rs.setFetchSize(fetchSize);
}
catch (Throwable t) {
// Ignore. Exception may happen if the driver doesn't support
// this operation and we didn't set meta-data correctly.
// However, it is a good idea to update the meta-data so that
// we don't have to incur the cost of catching an exception
// each time.
}
}
}
/**
* Uses a connection from the database to set meta data information about
* what different JDBC drivers and databases support.
*/
private static void setMetaData(Connection con) throws SQLException {
DatabaseMetaData metaData = con.getMetaData();
// Supports transactions?
supportsTransactions = metaData.supportsTransactions();
// Supports subqueries?
supportsSubqueries = metaData.supportsCorrelatedSubqueries();
// Set defaults for other meta properties
streamLargeText = false;
supportsMaxRows = true;
supportsFetchSize = true;
// Get the database name so that we can perform meta data settings.
String dbName = metaData.getDatabaseProductName().toLowerCase();
String driverName = metaData.getDriverName().toLowerCase();
// Oracle properties.
if (dbName.indexOf("oracle") != -1) {
databaseType = DatabaseType.ORACLE;
streamLargeText = true;
// The i-net AUGURO JDBC driver
if (driverName.indexOf("auguro") != -1) {
streamLargeText = false;
supportsFetchSize = true;
supportsMaxRows = false;
}
}
// Postgres properties
else if (dbName.indexOf("postgres") != -1) {
supportsFetchSize = false;
}
// Interbase properties
else if (dbName.indexOf("interbase") != -1) {
supportsFetchSize = false;
supportsMaxRows = false;
}
// SQLServer, JDBC driver i-net UNA properties
else if (dbName.indexOf("sql server") != -1 &&
driverName.indexOf("una") != -1)
{
supportsFetchSize = true;
supportsMaxRows = false;
}
// MySQL properties
else if (dbName.indexOf("mysql") != -1) {
databaseType = DatabaseType.MYSQL;
}
}
/**
* Returns the database type. The possible types are constants of the
* DatabaseType class. Any database that doesn't have its own constant
* falls into the "Other" category.
*
* @return the database type.
*/
public static DatabaseType getDatabaseType() {
return databaseType;
}
/**
* Returns true if connection profiling is turned on. You can collect
* profiling statistics by using the static methods of the ProfiledConnection
* class.
*
* @return true if connection profiling is enabled.
*/
public static boolean isProfilingEnabled() {
return profilingEnabled;
}
/**
* Turns connection profiling on or off. You can collect profiling
* statistics by using the static methods of the ProfiledConnection
* class.
*
* @param profilingEnabled true to enable profiling; false to disable.
*/
public static void setProfilingEnabled(boolean enable) {
// If enabling profiling, call the start method on ProfiledConnection
if (!profilingEnabled && enable) {
// ProfiledConnection.start();
}
// Otherwise, if turning off, call stop method.
else if(profilingEnabled && !enable) {
// ProfiledConnection.stop();
}
profilingEnabled = enable;
}
/**
* A class that identifies the type of the database that Jive is connected
* to. In most cases, we don't want to make any database specific calls
* and have no need to know the type of database we're using. However,
* there are certain cases where it's critical to know the database for
* performance reasons.
*/
public static class DatabaseType {
public static final DatabaseType ORACLE = new DatabaseType();
public static final DatabaseType MYSQL = new DatabaseType();
public static final DatabaseType OTHER = new DatabaseType();
private DatabaseType() {
/* do nothing */
}
}
/**
* Shuts down the current connection provider. It should be called when
* the VM is exiting so that any necessary cleanup can be done.
*/
private static class ShutdownThread extends Thread {
public void run() {
ConnectionProvider provider = ConnectionManager.getConnectionProvider();
if (provider != null) {
provider.destroy();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -