source:
http://blog.9cbs.net/sylmoon/archive/2005/02/03/279429.aspx
Originally, it is possible to write a small database application with JSP. Do not satisfy the JAVA complete source code, so it is very depressed when I wrote, I am very depressed, I am more annoying, servlet is also, OUT .println ("") is more depressed. Not simple things are always uncomfortable, and simple things do not exist, even if they pursue simple things, the process is so difficult! (This may be human A basic contradiction facing the feelings, from last week, have been working on Struts, and learn is very depressed, the information is small, and the copy code is verified to a data connection. I encountered trouble: How to create a data connection Pool 2. Configure the STURTS database connection pool. I touched this afternoon nail yesterday afternoon. Whether it is Google Search or I don't know how to read the Apache website, the effective information is very small, and the CADN has sent an admission, there is no People reply! This may be a master that disdain, but the rookie, but the problem is nematic?
I want to go, I feel that I don't know the data connection pool at all, so let's learn, just a chapter, it will not be very painful.
Think about the normal JDBC connection data first:
String Dirver = Oracle.jdbc.driver.Oracledriver;
//(com.mysql:jdbc.driver);
String username = ""
String password = ""
String Url = "JDBC: Oracle: Thin: @ 192.168.56.199: 1521: ASDB"
//jdbc:mysql://192.168.56.6:3306/mysql
String query = "SELECT * FROM EMP"
Connection conn = NULL;
Class.Forname (driver);
Connection = DriverManager.getConnection (URL, Username, Password);
Statement st = conn.createment (); (PreparedStatement)
ResultSet RS = St.executeQuery (Query);
While (rs.next ())
{do Something;
}
CONN.CLOSE ();
There is also in the Try / Catch block, caught SQLException, of course, can be closed with Finally to close CONN.
There is also a connection method, the following steps are the same, the front is written:
Connext CTX = New InitailContext ();
DataSource DS = (Datasource) CTX.Loolup ("JDBC / ASDBDB");
// SID DB
Connection conn = ds.getConnection (user, PWD);
This is the establishment of a data source connection.
Then learn Oracle's data sources and connecting pools
Import java.naming. *;
DataSource // DataSource is the factory of Connection
OracleDataSource // Oracle DataSource Version
ConnectionPoolDataSource // ConnectionPoolDataSource is the factory of PooledConnection
PooledConnection / / PooledConnection object is a connection object for connecting pool management
Steps:
STEP 1: Create a connection pool data source object
OracleConnectionPoolDataSource
Myocpds = new OracleConnectionPoolDatesource ()
STEP 2.
Set connection properties
Myocpds.setServerName ("Aaron");
Myocpds.SetDatabaseName ("moon");
Myocpds.setportNumber (1521);
Myocpds.SetDrivertype ("Thin");
STEP3
Create a buffer connection object
PooledConnection MyPooledConnection = Myocpds.SetPooledConnection ();
STEP 4
Request / Use Close Connection
Connection myconnection = mypooledConnection.getConnection ();
// Establish a connection using the object created in the previous step
MyConnection.Close ();
STEP 5
Turn off the buffer connection
mypooledConnection.Close ();
Oracle JDBC connection cache
The connection cache is a collection of multiple top connections, which utilizes a connection buffer and does not require the developer processing buffer object (PooledConnecion)
// Step: 1
OracleConnenctionCacheImpl
Myocci = new oracleConnectionCacheImpl ();
// Step: 2
Then the same STEP2
// Step: 3
.... No need to develop people to deal with PooledConnection
// Step: 4
CONNECTION myconnection = myoccl.getConnection ();
Myocci.getActiveSize (); number of PooledConnection objects
Close connection:
MyConnection.Close ();
// Step: 5
Turn off the connection cache
Myocci.close (); these processes similar to the establishment of connecting buffers above
Control the number of PooledConnection objects
Myocci.setMaxLimit (6); maximum number of PooledConnection objects
When the maximum number is reached, there are 3 cache modes:
Dynamic: Default, create a heart's PooledConnection object
Fixed Wait: Waiting for idle
Fixed with no wait: Rejected.
Myocci.setminlimit (3); at least 3 PooledConnection objects