📄 connmanager.java
字号:
package com.trulytech.mantis.system;import java.sql.Connection;import java.sql.DriverManager;import javax.naming.Context;import javax.naming.InitialContext;import javax.sql.DataSource;import java.sql.SQLException;/** * <p>Title: ConnManager</p> * <p>Description: 数据库连接管理</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: trulytech</p> * @author WangXian * @version 1.2 */public class ConnManager {//是否已经初始化 public static boolean isInited = false;//数据源 private static DataSource ds = null;//JDBC属性 private static java.util.Properties props = null; /** * 获得数据库连接 * @return Connect 数据库连接 * @throws Exception */ public static Connection getConnection() throws Exception { Connection conn = null; if (!isInited) throw new Exception("Database Connection has not been initilized!"); try { //如果是native的连接池 if (Properties.isConnectPool && Properties.PoolProvider.equalsIgnoreCase("native")) { conn = PoolManager.getInstance().getConnection(); if (conn == null)throw new ConnectionException(); return conn; } //如果使用tomcat的连接池 else if (Properties.isConnectPool && Properties.PoolProvider.equalsIgnoreCase("tomcat")) { conn = ds.getConnection(); if (conn == null)throw new ConnectionException(); return conn; } //如果使用非tomcat的连接池 else if (Properties.isConnectPool && !Properties.PoolProvider.equalsIgnoreCase("tomcat")) { conn = ds.getConnection(Properties.UserName, Properties.Password); if (conn == null)throw new ConnectionException(); return conn; } //如果不使用连接池 else { conn = DriverManager.getConnection(Properties.JDBCURL, props); if (conn == null)throw new ConnectionException(); return conn; } } catch (Exception e) { logWriter.Error("获取数据库连接失败-" + e.toString()); throw e; } } /** * 关闭数据库连接 * @param conn Connection 连接 * @throws SQLException */ public static void closeConnection(Connection conn) throws SQLException { if (conn != null) { //如果使用native Connection Pool if (Properties.isConnectPool && Properties.PoolProvider.equalsIgnoreCase("native")) { PoolManager.getInstance().freeConnection(conn); } else { if (!conn.isClosed()) { conn.close(); conn = null; } } } } /** * 释放所有数据库连接 */ public static void releaseConnection() { if (Properties.isConnectPool && Properties.PoolProvider.equalsIgnoreCase("native")) { PoolManager.getInstance().release(); logWriter.Info("连接池释放连接成功"); } } /** * 初始化数据库连接 * @throws Exception */ protected static void init() throws Exception { if (isInited) return; try { props = new java.util.Properties(); props.put("characterEncoding", Properties.Charset); props.put("useUnicode", Properties.isUnicode); props.put("user", Properties.UserName); props.put("password", Properties.Password); //如果使用native Connection Pool if (Properties.isConnectPool && Properties.PoolProvider.equalsIgnoreCase("native")) { logWriter.Debug("使用内部连接池"); Class.forName(Properties.JDBCClass); logWriter.Info("数据库连接初始化成功"); isInited = true; } //如果使用Weblogic连接池 else if (Properties.isConnectPool && !Properties.PoolProvider.equalsIgnoreCase("native")) { if (!Properties.PoolProvider.equalsIgnoreCase("tomcat")) { logWriter.Debug("使用外部连接池"); Context ctx = new InitialContext(); ds = (DataSource) ctx.lookup(Properties.DataSource); } //如果使用Tomcat DBCP连接池 else { logWriter.Debug("使用Tomcat连接池"); Context ctx = new InitialContext(); Context envContext = (Context) ctx.lookup("java:/comp/env"); ds = (DataSource) envContext.lookup(Properties.DataSource); } if (ds != null) { logWriter.Info("数据库连接池初始化成功"); isInited = true; } else { throw new Exception("ConnectPool Established Failure!"); } } //如果直接连接 else { logWriter.Debug("使用数据库直接连接"); Class.forName(Properties.JDBCClass); logWriter.Info("数据库连接初始化成功"); isInited = true; } } catch (Exception e) { logWriter.Error("数据库连接池初始化失败-" + e.toString()); throw e; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -