connectionmanager.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 493 行 · 第 1/2 页
JAVA
493 行
/** * $RCSfile: ConnectionManager.java,v $ * $Revision: 1.14 $ * $Date: 2002/07/08 00:21:53 $ * * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.database;import java.sql.*;import java.io.*;import com.jivesoftware.forum.*;/** * Central manager of database connections. All methods are static so that they * can be easily accessed throughout the classes in the database package.<p> * * This class also provides a set of utility methods that abstract out * operations that may not work on all databases such as setting the max number * or rows that a query should return. * * @see ConnectionProvider */public class ConnectionManager { private static ConnectionProvider connectionProvider; private static Object providerLock = new Object(); // True if connection profiling is turned on. Always false by default. private static boolean profilingEnabled = false; // True if the database support transactions. protected static boolean supportsTransactions; // True if the database requires large text fields to be streamed. protected static boolean streamLargeText; // True if the database supports the Statement.setMaxRows() method. protected static boolean supportsMaxRows; // True if the database supports the Statement.setFetchSize() method. protected static boolean supportsFetchSize; // True if the database supports correlated subqueries. protected static boolean supportsSubqueries; // True if the database supports scroll-insensitive results. protected static boolean supportsScrollResults; private static DatabaseType databaseType = DatabaseType.OTHER; /** * Returns a database connection from the currently active connection * provider. */ public static Connection getConnection() throws SQLException { if (connectionProvider == null) { synchronized (providerLock) { if (connectionProvider == null) { // Attempt to load the connection provider classname as // a Jive property. String className = JiveGlobals.getJiveProperty("connectionProvider.className"); if (className != null) { // Attempt to load the class. try { Class conClass = Class.forName(className); setConnectionProvider((ConnectionProvider)conClass.newInstance()); } catch(Exception e) { e.printStackTrace(); System.err.println("Warning: failed to create the " + "connection provider specified by connection" + "Provider.className. Using the default pool."); setConnectionProvider(new DefaultConnectionProvider()); } } else { setConnectionProvider(new DefaultConnectionProvider()); } } } } Connection con = connectionProvider.getConnection(); if (con == null) { System.err.println("WARNING: ConnectionManager.getConnection() " + "failed to obtain a connection."); } // See if profiling is enabled. If yes, wrap the connection with a // profiled connection. if (profilingEnabled) { return new ProfiledConnection(con); } else { return con; } } /** * Returns a Connection from the currently active connection provider that * is ready to participate in transactions (auto commit is set to false). */ public static Connection getTransactionConnection() throws SQLException { Connection con = getConnection(); if (supportsTransactions) { con.setAutoCommit(false); } return con; } /** * Closes a Connection. However, it first rolls back the transaction or * commits it depending on the value of <code>abortTransaction</code>. */ public static void closeTransactionConnection(Connection con, boolean abortTransaction) { // Rollback or commit the transaction if (supportsTransactions) { try { if (abortTransaction) { con.rollback(); } else { con.commit(); } } catch (Exception e) { e.printStackTrace(); } } try { // Reset the connection to auto-commit mode. if (supportsTransactions) { con.setAutoCommit(true); } } catch (Exception e) { e.printStackTrace(); } try { // Close the db connection. con.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Creates a scroll insensitive Statement if the JDBC driver supports it, or a normal * Staement otherwise. * * @param con the database connection. * @return a Statement * @throws SQLException if an error occurs. */ public static Statement createScrollableStatement(Connection con) throws SQLException { if (supportsScrollResults) { return con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); } else { return con.createStatement(); } } /** * Creates a scroll insensitive PreparedStatement if the JDBC driver supports it, or a normal * PreparedStaement otherwise. * * @param con the database connection. * @param sql the SQL to create the PreparedStatement with. * @return a PreparedStatement * @throws SQLException if an error occurs. */ public static PreparedStatement createScrollablePreparedStatement(Connection con, String sql) throws SQLException { if (supportsScrollResults) { return con.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); } else { return con.prepareStatement(sql); } } /** * Scrolls forward in a result set to the specified row number. If the JDBC driver supports * the feature, the cursor will be moved directly. Otherwise, we scroll through results one by * one manually by calling <tt>rs.next()</tt>. * * @param rs the ResultSet object to scroll. * @param rowNumber the row number to scroll forward to. * @throws SQLException if an error occurs. */ public static void scrollResultSet(ResultSet rs, int rowNumber) throws SQLException { // If the driver supports scrollable result sets, use that feature. if (supportsScrollResults) { if (rowNumber > 0) { rs.setFetchDirection(ResultSet.FETCH_FORWARD); rs.absolute(rowNumber); } } // Otherwise, manually scroll to the correct row. else { for (int i=0; i<rowNumber; i++) { rs.next(); } } } /** * Returns the current connection provider. The only case in which this * method should be called is if more information about the current * connection provider is needed. Database connections should always be * obtained by calling the getConnection method of this class. */ public static ConnectionProvider getConnectionProvider() { return connectionProvider; } /** * Sets the connection provider. The old provider (if it exists) is shut * down before the new one is started. A connection provider <b>should * not</b> be started before being passed to the connection manager * because the manager will call the start() method automatically. * * @param provider the ConnectionProvider that the manager should obtain * connections from. */ public static void setConnectionProvider(ConnectionProvider provider) { synchronized (providerLock) { if (connectionProvider != null) { connectionProvider.destroy(); connectionProvider = null; } connectionProvider = provider; connectionProvider.start(); // Now, get a connection to determine meta data. Connection con = null; try { con = connectionProvider.getConnection(); setMetaData(con); } catch (Exception e) { e.printStackTrace(); } finally { try { con.close(); } catch (Exception e) { } } } // Remember what connection provider we want to use for restarts.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?