Danh mục

Java Server Pages: A Code-Intensive Premium Reference- P14

Số trang: 10      Loại file: pdf      Dung lượng: 143.34 KB      Lượt xem: 10      Lượt tải: 0    
Hoai.2512

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Java Server Pages: A Code-Intensive Premium Reference- P14:Before you begin reading Pure JSP Java Server Pages, you might want to take a look at its basicstructure. This should help you outline your reading plan if you choose not to read the text from cover tocover. This introduction gives you an overview of what each chapter covers.
Nội dung trích xuất từ tài liệu:
Java Server Pages: A Code-Intensive Premium Reference- P14 } } } A JDBC Connection Pool To make use of our pooled connections, we need a ConnectionPool object to manage them. The requirements for our ConnectionPool object are1. It must hold n number of open connections.2. It must be able to determine when a connection is in use.3. If n+1 connections are requested, it must create a new connection and add it to the pool.4. When we close the pool, all connections must be released. Now that we know what we want, lets look at what we came up with. The source for our ConnectionPool object is in Listing 14.2. Listing 14.2: ConnectionPool.java package com.purejsp.connectionpool; import java.sql.*; import java.util.*; public class ConnectionPool { // JDBC Driver Name private String driver = null; // URL of database private String url = null; // Initial number of connections. private int size = 0; // Username private String username = new String(); // Password private String password = new String(); // Vector of JDBC Connections private Vector pool = null; - 131 -public ConnectionPool() {}// Set the value of the JDBC Driverpublic void setDriver(String value) { if ( value != null ) { driver = value; }}// Get the value of the JDBC Driverpublic String getDriver() { return driver;}// Set the URL Pointing to the Datasourcepublic void setURL(String value ) { if ( value != null ) { url = value; }}// Get the URL Pointing to the Datasourcepublic String getURL() { - 132 - return url;}// Set the initial number of connectionspublic void setSize(int value) { if ( value > 1 ) { size = value; }}// Get the initial number of connectionspublic int getSize() { return size;}// Set the usernamepublic void setUsername(String value) { if ( value != null ) { username = value; }}// Get the usernamepublic String getUserName() { - 133 - return username;}// Set the passwordpublic void setPassword(String value) { if ( value != null ) { password = value; }}// Get the passwordpublic String getPassword() { return password;}// Creates and returns a connectionprivate Connection createConnection() throws Exception { Connection con = null; // Create a Connection con = DriverManager.getConnection(url, username, password); return con;}// Initialize the pool - 134 -public synchronized void initializePool() throws Exception { // Check our initial values if ( driver == null ) { throw new Exception(No Driver Name Specified!); } if ( url == null ) { throw new Exception(No URL Specified!); } if ( size < 1 ) { throw new Exception(Pool size is less than 1!); } // Create the Connections try { // Load the Driver class file Class.forName(driver); // Create Connections based on the size member for ( int x = 0; x < size; x++ ) { Connection con = createConnection(); if ( con != null ) { // Create a PooledConnection to encapsulate the // real JDBC Connection - 135 - PooledConnection pcon = new PooledConnection(con); // Add the Connection to the pool. addConnection(pcon); } } } catch (Exception e) { System.err.println(e.getMessage()); throw new Exception(e.getMessage()); }}// Adds the PooledConnection to the poolprivate void addConnection(PooledConnection value) { // If the pool is null, create a new vector // with the initial size of size if ( pool == null ) { pool = new Vector(size); } // Add the PooledConnection Object to the vector pool.addElement(value);}public synchronized void releaseConnection(Connection con) { // find the PooledConnection Object for ( int x = 0; x < pool.size(); x++ ) { - 136 - PooledConnection pcon = (PooledConnection)pool.elementAt(x); // Check for correct Connection if ( pcon.getConnection() == con ) { System.err.println(Releasing Connection + x); // Set its inuse attribute to false, which // releas ...

Tài liệu được xem nhiều: