Package com.action;
Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Org.apache.tomcat.jdbc.pool.DataSource;
Import org.apache.tomcat.jdbc.pool.PoolProperties; /* * Tomcat's own JDBC Pool connection pooling technology is said to be more efficient and will be used today to sort out the following: * Requires JAR package support: Tomcat-juli.jar * There are two ways to use this pool operation, one is to configure parameters in the project, and one to be like dbcp in con Text.xml File Configuration * First: Directly in the project file configuration, here omitted the contents of the configuration file, directly put the parameters into the Java code; (using such a pool operation in a standalone application) * */public class Tomcat_jdbc_pool {Publ IC static void Main (string[] args) {poolproperties p = new Poolproperties (); Pool Management Object Instantiation P.seturl ("Jdbc:mysql://localhost:3306/jack"); Set the URL p.setdriverclassname ("Com.mysql.jdbc.Driver");//Set the driver p.setusername ("root");//Set the user name P.setpassword ("1");// Set Password p.setjmxenabled (true);//Set whether the Java Management extension is available to support JMX P.settestwhileidle (FALSE);//Set Idle If idle object collector is enabled P.settestonbor Row (true);//The Fetch connection checks the validity of the link when a pool instance is borrow, whether the pool operation is done in advance, and if true, the resulting pool instance is available; P.settestonreturn (false);//test the connection for a return P.S Etvalidationquery ("Select 1 ");//Verify the connection validity, this step can not save the test connection statement p.setvalidationinterval (30000);//authentication interval time//timebetweenevictionrunsmillis and Minevictableidletimemillis,//Their two mates, can continuously update connection objects in the connection pool,//when Timebetweenevictionrunsmillis is greater than 0 o'clock,
Every timebetweenevictionrunsmillis time,//A thread is started to verify connection objects that have been idle for more than Minevictableidletimemillis in the connection pool.
P.settimebetweenevictionrunsmillis (30000);
P.setminevictableidletimemillis (30000); P.setmaxactive (100);//connection pool maximum concurrent capacity p.setinitialsize (10);//When initializing connection pool, create connection number p.setmaxwait (10000);//timeout wait time in milliseconds p.setre Moveabandonedtimeout (60);//timeout (in seconds) p.setmaxidle (100);//maximum number of idle connections P.setminidle (10);//minimum number of idle connections p.setlogabandoned ( true); Whether to print the connection Timeout error p.setremoveabandoned (true) when the time-out connection is automatically reclaimed,//whether the auto-recycle timeout connection//setup JDBC Interceptor p.setjdbcinterceptors ("org.apache.t Omcat.jdbc.pool.interceptor.ConnectionState; "
+ "Org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"); DataSource DataSource = new DataSource ();//tomcat data source datasource.setpoolproperties (p); Pool and data source bindingFixed Connection con = null;
try {con = datasource.getconnection ();
Statement st = Con.createstatement ();
ResultSet rs = st.executequery ("SELECT * from student"); while (Rs.next ()) {System.out.println (".
Host: "+ rs.getstring (2) +" Password: "+ rs.getstring (3));
} rs.close ();
St.close ();
} catch (SQLException e) {e.printstacktrace ();
} finally {if (con! = null) try {con.close (); } catch (Exception ignore) {}}}}
If you use this technique as a global technique, you will need to write the configuration information in the context.xml file of the Tomcat Conf folder (parameters refer to the comments above):
<resource
name= "Jdbc/testdb"
auth= "Container"
type= "Javax.sql.DataSource"
factory= " Org.apache.tomcat.jdbc.pool.DataSourceFactory "
testwhileidle=" true "
testonborrow=" true "
Testonreturn= "false"
validationquery= "Select 1"
validationinterval= "30000"
Timebetweenevictionrunsmillis= "30000"
maxactive= "
minidle="
maxwait=
"10000" Initialsize= "Ten"
removeabandonedtimeout= "
removeabandoned=" true "
logabandoned=" true "
Minevictableidletimemillis= "30000"
jmxenabled= "true"
jdbcinterceptors= " Org.apache.tomcat.jdbc.pool.interceptor.connectionstate;o Rg.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer "
username=" root "
password=" password "
Driverclassname= "Com.mysql.jdbc.Driver"
url= "Jdbc:mysql://localhost:3306/mysql"/>
Note: Get the connection asynchronously
1. Fairqueue must be set to True
2. The data source must be converted to Org.apache.tomcat.jdbc.pool.DataSource
The connection pool gets
Connection conn = null;
DataSource ds = null;
ResultSet rs =null;
Statement stmt = null;
Context initctx = new InitialContext ();
ds = (DataSource) initctx.lookup ("Java:comp/env/jdbc/ens");
if (ds!=null) {
out.println ("already obtained datasource!");
Out.println ("<br>");
conn = Ds.getconnection ();
try{
stmt = Conn.createstatement ();
String sql = "SELECT * from Ens_area";
rs = stmt.executequery (sql);
Out.println ("The following is the data read from the database:<br>");
while (Rs.next ()) {
out.println ("<br>");
Out.println (rs.getstring ("Area_name"));
}
} catch (Exception ex) {
ex.printstacktrace ();
} finally{
conn.close ();
Rs.close ();
Stmt.close ();
}
}