ConnectionPooljar数据连接池文件包的制作流程规范规范_第1页
ConnectionPooljar数据连接池文件包的制作流程规范规范_第2页
ConnectionPooljar数据连接池文件包的制作流程规范规范_第3页
ConnectionPooljar数据连接池文件包的制作流程规范规范_第4页
ConnectionPooljar数据连接池文件包的制作流程规范规范_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、关于数据连接池文件包的制作流程目的:制作通用型数据连接池文件包,便于应用于通用工程结构:工程包内classes目录内 :连接池管理程序|2. com :MySql JDBC驱动程序3. org :MySql JDBC驱动程序4. net :MSSqlserver JTDS 驱动程序5. javax :Oracle JDBC 驱动程序6. oracle :Oracle JDBC 驱动程序制作流程:新建一个工程命名为Pool,在工程内建立一个war模块命名为Poolwar;在工程内新建class文件,文件的包起名为ConnectionPool;依次新建下列文件:、将Oracle for JDBC驱

2、动文件用winrar解压缩,得到javax和oracle两个文件夹,将这两个文件夹复制到Pool工程内的classes目录下;将MS-Sqlserver for JDBC驱动文件用winrar解压缩,得到net这个文件夹,将这个文件夹复制到Pool工程内的classes目录下;将MySql for JDBC驱动文件用winrar解压缩,得到com和org两个文件夹,将这两个文件夹复制到Pool工程内的classes目录下;如果需要添加其他驱动程序,可参照4-6进行;选择JBuilder工具条上Wizards-Archive Builder ; Builder Step 1 of 5 : Arc

3、hive Type 选择Basic(具体类型含义参考JBuilerX使用手册);10. Archive Builder Step 2 of 5 : 将Name 和 File 命名为需要的名字,这里同意将其命名为ConnectionPool,其他的选项均不做更改;11Archive Builder Step 3 of 5 : 指定结构文件包含内容,这里不做任何修改;12Archive Builder Step 4 of 5 : 将servlet 选择为 Allways include all classes and resources;13Archive Builder Step 4 of 5

4、: 保持默认状态不改变,按finish完成结构文件的定义;14在工程窗口用鼠标右键点 ConnectionPool 结构,然后选择make 生成结构文件;使用方法:将该jar文件添加到 ToolsConfigure Librarise 中;新加工程,在工程 Required Librarise 中添加该库文件;在工程中新建一个“连接池初始化类”负责建立数据连接;在工程中新建一属性文件,负责描述数据库驱动及用户信息等,该属性文件存放的位置在中定义;在中默认为“./ ”,该位置表示在/WEB-INF 目录下;附录:文件代码*文件开始* ;import .*;import .*;public cla

5、ss ConnectionPool private String name; private String URL; private String user; private String password; private int maxConns; private int timeOut; private LogWriter logWriter; private int checkedOut; private Vector freeConnections = new Vector(); public ConnectionPool(String name, String URL, Strin

6、g user, String password, int maxConns, int initConns, int timeOut, PrintWriter pw, int logLevel) = name; = URL; = user; = password; = maxConns; = timeOut 0 timeOut : 5; logWriter = new LogWriter(name, logLevel, pw); initPool(initConns); (New pool created, ; String lf = (); ( url= + URL + user= + use

7、r + long startTime = (); long remaining = timeout; Connection conn = null; while (conn = getPooledConnection() = null) try (Waiting for connection. Timeout= + remaining, ; wait(remaining); catch (InterruptedException e) remaining = timeout - () - startTime); if (remaining = 0) Try again with the rem

8、aining timeout (Removed selected bad connection from pool, ; return getConnection(remaining); checkedOut+; (Delivered getConnection from pool, ; (getStats(), ; return conn; private boolean isConnectionOK(Connection conn) Statement testStmt = null; try if (!() ;import .*;/* * This class is a wrapper

9、around a Connection, overriding the * close method to just inform the pool that its available for * reuse again, and the isClosed method to return the state * of the wrapper instead of the Connection. */class ConnectionWrapper implements Connection */ public void close() throws SQLException isClosed

10、 = true; (realConn); /* * Returns true if the ConnectionWrapper is closed, false * otherwise. */ public boolean isClosed() throws SQLException return isClosed; /* * Wrapped methods. */ public void clearWarnings() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed);

11、(); public void commit() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); (); public Statement createStatement() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return (); public boolean getAutoCommit() throws SQLException

12、if (isClosed) throw new SQLException(Pooled connection is closed); return (); public String getCatalog() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return (); public DatabaseMetaData getMetaData() throws SQLException if (isClosed) throw new SQLException(Po

13、oled connection is closed); return (); public int getTransactionIsolation() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return (); public SQLWarning getWarnings() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return

14、(); public boolean isReadOnly() throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return (); public String nativeSQL(String sql) throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return (sql); public CallableStatement prepareC

15、all(String sql) throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return (sql); public PreparedStatement prepareStatement(String sql) throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); return (sql); public void rollback() throw

16、s SQLException if (isClosed) throw new SQLException(Pooled connection is closed); (); public void setAutoCommit(boolean autoCommit) throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); (autoCommit); public void setCatalog(String catalog) throws SQLException if (isCl

17、osed) throw new SQLException(Pooled connection is closed); (catalog); public void setReadOnly(boolean readOnly) throws SQLException if (isClosed) throw new SQLException(Pooled connection is closed); (readOnly); public void setTransactionIsolation(int level) throws SQLException if (isClosed) throw ne

18、w SQLException(Pooled connection is closed); (level); public void setHoldability(int holdability) throws SQLException (holdability); public int getHoldability() throws SQLException return (); public Savepoint setSavepoint() throws SQLException return (); public Savepoint setSavepoint(String name) th

19、rows SQLException return (name); public void releaseSavepoint(Savepoint savepoint) throws SQLException (savepoint); public void rollback(Savepoint savepoint) throws SQLException (savepoint); public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throw

20、s SQLException return (resultSetType,resultSetConcurrency,resultSetHoldability); public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException return (sql,resultSetConcurrency,resultSetHoldability); public CallableSta

21、tement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException return (sql,resultSetConcurrency,resultSetHoldability); public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException return (sql,autoGenerated

22、Keys); public PreparedStatement prepareStatement(String sql, int columnIndexes) throws SQLException return (sql,columnIndexes); public PreparedStatement prepareStatement(String sql, String columnNames) throws SQLException return (sql,columnNames); public Statement createStatement(int resultSetType,

23、int resultSetConcurrency) throws SQLException return (resultSetType,resultSetConcurrency); public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException return (sql,resultSetType ,resultSetConcurrency); public CallableStatement prepareCall(Str

24、ing sql, int resultSetType, int resultSetConcurrency) throws SQLException return (sql,resultSetType,resultSetConcurrency); public Map getTypeMap() throws SQLException return (); public void setTypeMap(Map map) throws SQLException (map); *文件结束*文件开始*package ConnectionPool;import .*;import .*;public cl

25、ass LogFile private static String myDir = c:test; private static String myFile = ; public void LogFile() public static void log(String s) try MakeDateTime qdt = new MakeDateTime(); String dateString = (); String stemp = (); String filename = myDir + HK + stemp + .log; BufferedWriter out = new Buffer

26、edWriter(new FileWriter(filename, true); (dateString + : + s); (n); (); catch(Exception e) ; public static void log(String _dir, String s) try MakeDateTime qdt = new MakeDateTime(); String dateString = (); String stemp = (); String filename = _dir; BufferedWriter out = new BufferedWriter(new FileWri

27、ter(filename, true); (dateString + : + s); (n); (); catch(Exception e) ; *文件结束*文件开始*package ConnectionPool;import .*;import .*;public class LogWriter public static final int NONE = 0; public static final int ERROR = 1; public static final int INFO = 2; public static final int DEBUG = 3; private stat

28、ic final String ERROR_TEXT = error; private static final String INFO_TEXT = info; private static final String DEBUG_TEXT = debug; private PrintWriter pw; private String owner; private int logLevel; public LogWriter(String owner, int logLevel, PrintWriter pw) = pw; = owner; = logLevel; public LogWrit

29、er(String owner, int logLevel) this(owner, logLevel, null); public int getLogLevel() return logLevel; public PrintWriter getPrintWriter() return pw; public void setLogLevel(int logLevel) = logLevel; public void setPrintWriter(PrintWriter pw) = pw; public void log(String msg, int severityLevel) if (p

30、w != null) if (severityLevel + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = ()

31、; = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = (); = + (); String Str = ()

32、; = + (); *文件结束*文件开始*package ConnectionPool;import .*;import .*;import .*;public class PoolManager static private PoolManager instance; static private int clients; private LogWriter logWriter; private PrintWriter pw; private Vector drivers = new Vector(); private Hashtable pools = new Hashtable(); p

33、rivate PoolManager() init(); static synchronized public PoolManager getInstance() if (instance = null) instance = new PoolManager(); clients+; return instance; private void init() etResourceAsStream(./); Properties dbProps = new Properties(); try (is); catch (Exception e) + Make sure is in the CLASS

34、PATH, ; return; String logFile = (logfile); if (logFile != null) try pw = new PrintWriter(new FileWriter(logFile, true), true); (pw); catch (IOException e) (Cant open the log file: + logFile + . Using instead, ; loadDrivers(dbProps); createPools(dbProps); private void loadDrivers(Properties props) S

35、tring driverClasses = (drivers); StringTokenizer st = new StringTokenizer(driverClasses); while () String driverClassName = ().trim(); try Driver driver = (Driver) (driverClassName).newInstance(); (driver); (driver); (Registered JDBC driver + driverClassName,; catch (Exception e) (e, Cant register J

36、DBC driver: + driverClassName, ; private void createPools(Properties props) Enumeration propNames = (); while () String name = (String) (); if (.url) String poolName = (0, (.); String url = (poolName + .url); if (url = null) (No URL specified for + poolName,; continue; String user = (poolName + .use

37、r); String password = (poolName + .password); String maxConns = (poolName+.maxconns, 0); int max; try max = (maxConns).intValue(); catch (NumberFormatException e) (Invalid maxconns value + maxConns + for + poolName, ; max = 0; String initConns = (poolName +.initconns, 0); int init; try init = (initC

38、onns).intValue(); catch (NumberFormatException e) (Invalid initconns value + initConns + for + poolName, ; init = 0; String loginTimeOut = (poolName + .logintimeout, 5); int timeOut; try timeOut = (loginTimeOut).intValue(); catch (NumberFormatException e) (Invalid logintimeout value + loginTimeOut +

39、 for + poolName, ; timeOut = 5; String logLevelProp = (poolName +.loglevel, ); int logLevel = ; if (none) logLevel = ; else if (error) logLevel = ; else if (debug) logLevel = ; ConnectionPool pool = new ConnectionPool(poolName, url, user, password, max, init, timeOut, pw, logLevel); (poolName, pool); public Connection getConnection(String na

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论