Database link pool--a simple understanding

Source: Internet
Author: User

Commons-logging This bag can not

 Packagerocky.pool.dbcp;Importjava.sql.Connection;Importjava.sql.SQLException;ImportOrg.apache.commons.dbcp.BasicDataSource; Public classtestdbcp{ Public Static voidMain (string[] args)throwssqlexception{//get the data source object//DataSource (Standard interface) Basicdatasource (Apache implementation)Basicdatasource ds =NewBasicdatasource (); Ds.seturl ("Jdbc:mysql://localhost:3306/hibernate"); Ds.setdriverclassname ("Com.mysql.jdbc.Driver"); Ds.setusername ("Root"); Ds.setpassword ("2011211961"); //ds.setmaxactive (4);//maximum number of activities//Ds.setmaxidle (2);//maximum number of saved//ds.setmaxwait (10000);//Maximum wait timeConnection conn1 =ds.getconnection (); System.out.println ("Conn" +conn1); Connection conn2=ds.getconnection (); System.out.println ("Conn" +conn2); Connection conn3=ds.getconnection (); System.out.println ("Conn" +conn3);       Conn1.close ();       Conn2.close ();       Conn3.close (); System.out.println ("----------------------"); Connection Conn4=ds.getconnection (); System.out.println ("Conn" +conn4); Connection Conn5=ds.getconnection (); System.out.println ("Conn" +conn5); Connection conn6=ds.getconnection (); System.out.println ("Conn" +conn6); Conn4.close ();
Conn5.close ();
Conn6.close (); } }

The result of the operation of this piece of code

Conn---E-Mail protected]----------------------- >->[email protected]

Now look at the top three like is not about the middle of the dashed line axisymmetric Ah, do not think stack ah

The program first creates three links,

Second, print conn

Perform related actions ignore

Again, close the Conn,

Second time get connection, print

The 1th connection that was printed the second time was the 3rd before the last shutdown

The 2nd connection that was printed the second time was the 2nd before the last shutdown

The 3rd connection that was printed the second time was the 1th before the last shutdown

, the connection pool is like a stack. First created existing, then closed on top of the stack

 Public Static voidMain (string[] args)throwssqlexception{//get the data source object//DataSource (Standard interface) Basicdatasource (Apache implementation)Basicdatasource ds =NewBasicdatasource (); Ds.seturl ("Jdbc:mysql://localhost:3306/hibernate"); Ds.setdriverclassname ("Com.mysql.jdbc.Driver"); Ds.setusername ("Root"); Ds.setpassword ("2011211961"); Ds.setmaxactive (4);//maximum number of activitiesDs.setmaxidle (2);//maximum number of savedDs.setmaxwait (10000);//Maximum wait timeConnection conn1 =ds.getconnection (); System.out.println ("Conn" +conn1); Connection conn2=ds.getconnection (); System.out.println ("Conn" +conn2); Connection conn3=ds.getconnection (); System.out.println ("Conn" +conn3);       Conn1.close ();       Conn2.close ();       Conn3.close (); System.out.println ("----------------------"); Connection Conn4=ds.getconnection (); System.out.println ("Conn" +conn4); Connection Conn5=ds.getconnection (); System.out.println ("Conn" +conn5); Connection conn6=ds.getconnection (); System.out.println ("Conn" +conn6); Conn4.close ();
Conn5.close ();
Conn6.close (); }

Run results

Conn---E-Mail protected]----------------------- >->[email protected]

Maximum number of saved

The maximum number of saves is set here, and the result is changed, only two of which are the same, if the number of fetches is greater than the maximum number of saved, you cannot get from the database connection pool, you can only recreate a

Maximum number of active and maximum wait times,

When the maximum number of saves has been reached, and the maximum number of activities is set, and the maximum connection time is set, when there is no conn in the database connection pool can wait for other places to close the conn, when the maximum waiting time exceeded the number of reported abnormal

 Public Static voidMain (string[] args)throwssqlexception{//get the data source object//DataSource (Standard interface) Basicdatasource (Apache implementation)Basicdatasource ds =NewBasicdatasource (); Ds.seturl ("Jdbc:mysql://localhost:3306/hibernate"); Ds.setdriverclassname ("Com.mysql.jdbc.Driver"); Ds.setusername ("Root"); Ds.setpassword ("2011211961"); Ds.setmaxactive (3);//maximum number of activitiesDs.setmaxidle (2);//maximum number of savedDs.setmaxwait (5000);//Maximum wait timeConnection conn1 =ds.getconnection (); System.out.println ("Conn" +conn1); Connection conn2=ds.getconnection (); System.out.println ("Conn" +conn2); Connection conn3=ds.getconnection (); System.out.println ("Conn" +conn3);       Conn1.close ();       Conn2.close ();       Conn3.close (); System.out.println ("----------------------"); Connection Conn4=ds.getconnection (); System.out.println ("Conn" +conn4); Connection Conn5=ds.getconnection (); System.out.println ("Conn" +conn5); Connection conn6=ds.getconnection (); System.out.println ("Conn" +conn6); Connection conn7=ds.getconnection (); System.out.println ("Conn" +conn7);       Conn4.close ();       Conn5.close ();       Conn6.close ();       Conn7.close (); }

Exception is reported when waiting for more than 5000 milliseconds

Conn[Email Protected]conn-[Email Protected]conn-[email protected]----------------------Conn-[Email Protected]conn-[Email Protected]conn-[email protected]exception in thread"Main"Org.apache.commons.dbcp.SQLNestedException:Cannot Get a connection, pool exhausted at Org.apache.commons.dbcp.Poo Lingdatasource.getconnection (Poolingdatasource.java:103) at Org.apache.commons.dbcp.BasicDataSource.getConnection (Basicdatasource.java:544) at Rocky.pool.dbcp.TestDbcp.main (Testdbcp.java:43) caused by:java.util.NoSuchElementException:Timeout waiting forIdle object at Org.apache.commons.pool.impl.GenericObjectPool.borrowObject (Genericobjectpool.java:756) at Org.apache.commons.dbcp.PoolingDataSource.getConnection (Poolingdatasource.java:95)    ... 2 more

How to configure the database connection pool in Tomcat

1. Add in the Tomcat\conf\context.xml file

2. Put the above three jar packages into the Tomcat Lib

<?XML version= ' 1.0 ' encoding= ' utf-8 '?><!--Licensed to the Apache software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License");  You are not a use of this file except in compliance with the License.  Obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 unless required by applicable or agreed to writing, software distributed under the License are distributed on a "as is" BASIS, without warranties  or CONDITIONS of any KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -<!--The contents of this file is loaded for each Web application -<Context>    <!--Default set of monitored resources. If One of these changes, the -    <!--Web application would be reloaded.  -    <Watchedresource>Web-inf/web.xml</Watchedresource>    <Watchedresource>${catalina.base}/conf/web.xml</Watchedresource>    <!--uncomment this to disable session persistence across Tomcat restarts -    <!--<manager pathname= ""/> -    <!--uncomment this to enable Comet connection tacking (provides events in session expiration as well as WebApp Li Fecycle) -    <!--<valve classname= "Org.apache.catalina.valves.CometConnectionManagerValve"/>- 

<resource name= "Ds_mysql"
Type= "Javax.sql.DataSource"
Maxactive= "maxidle=" maxwait= "5000"
Username= "root" password= "root"
Driverclassname= "Com.mysql.jdbc.Driver"
Url= "Jdbc:mysql://127.0.0.1:3306/tablename"/>
</Context>

Database link pool--a simple understanding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.