Original posts: http://www.cnblogs.com/HD/p/3627581.html
Step 1:
Locate the context.xml file under the Tomcat installation directory, under the Config directory. Under the <Context/> node, add:
<Resourcename= "Jdbc/mytest"Auth= "Container"type= "Javax.sql.DataSource"maxactive= "+"Maxidle= "Ten"maxwait= "-1"username=""Password=""Driverclassname= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"URL= "jdbc:sqlserver://127.0.0.1;databasename=ppmold;integratedsecurity=true;"/>
Step 2:
Put the original connection code object, for example:
Class.forName ("Com.microsoft.sqlserver.jdbc.SQLServerDriver"= drivermanager.getconnection ("JDBC: Sqlserver://127.0.0.1;databasename=ppmold;integratedsecurity=true; ");
Modified to:
New= (DataSource) initcontext.lookup ("java:comp/env/jdbc/mytest"= Ds.getconnection ();
Step 3:
Restart Tomcat.
The data says that you will add a piece of code to the Web. xml:
<Resource-ref> <Res-ref-name>Jdbc/mytest</Res-ref-name> <Res-type>Javax.sql.DataSource</Res-type> <Res-auth>Container</Res-auth> </Resource-ref>
This code has been tested without any problems.
Parameter description:
• Related parameter Description: DataSource: DataSource to be connected (usually we are not defined in Server.xml) Defaultautocommit: The default value is true for transactions autocommit Defaultreadonly: For database read only, the default value is False driverclassname: the JDBC Driver Class used to connect to the database, maxactive: The maximum number of objects that can be fetched from the object pool, 0 means no limit, the default is 8 Maxidle: The maximum number of waiting connections, set 0 is no limit (the maximum number of objects in the object pool) Minidle: The minimum number of objects in the object pool maxwait: The maximum wait seconds, in MS, the time will drop error message, 1 is unlimited p Assword: The password URL used to log into the database: the URL of the connection database username: The account used to log in to the database validationquery: Verify that the connection is successful, and that the SQL SELECT directive returns at least one row removeabandoned: is self-interrupting, the default is False removeabandonedtimeout: After a few seconds self-interrupts, removeabandoned must be true logabandoned: whether the interrupt event is logged, the default is False minevictabl Eidletimemillis: Greater than 0, the connection idle time is judged, or 0, the idle connection is not verified; default 30 minutes Timebetweenevictionrunsmillis: The Expiration check thread run time interval, if it is less than or equal to 0, Does not start the check thread, default-1 Testonborrow: Whether to validate when an object is obtained, check if the object is valid, default to False Testonreturn: whether to validate when returning an object, check if the object is valid, default to False Testwhileidle: Whether to verify when idle, check whether the object is valid, default to Falseø when using DBCP, if the default value is used, if the database connection is broken for some reason, then the connection from the connection pool is not verified. The connection that is made will actually be an invalid database connection. Therefore, in order to prevent the access to the database connection fails, it is best to ensure that: Username: Login Database validationquery:select COUNT (*) from DUAL Testonborrow, Testonreturn, Testwhileidle: Preferably set to true Minevictableidletimemillis: greater than 0, connection idle time is judged, or 0, idle connections are not verified Timebetweenevictionrunsmillis: Failure Check thread run interval, if less than equals 0, does not start check thread Øps: in the construction genericobjectpool [ Basicdatasource in its CreateDataSource () method also uses Genericobjectpool], an inline class Evictor is generated to implement the self-runnable interface. If Timebetweenevictionrunsmillis is greater than 0, every timebetweenevictionrunsmillis milliseconds Evictor calls the evict () method, Check if the object is idle for more than Minevictableidletimemillis milliseconds (_minevictableidletimemillis is less than or equal to 0 o'clock, default is 30 minutes), then destroy the object, or activate and validate the object. Then call the Ensureminidle method to check to make sure that the number of objects in the pool is not less than _minidle. When you call the Returnobject method to put the object back into the object pool, first check that the object is valid, and then call Poolableobjectfactory's Passivateobject method to make the object inactive. Then check whether the number of objects in the object pool is less than maxidle, it is possible to put this object back to the object pool, otherwise destroy this object o the above characteristics can be set up in the code to verify that the specific performance to be verified in the Tomcat Server.xml, we can look at the following example: In this case, you can avoid the error of connection reset. That way, you'll be able to solve the problem with connect reset. Just said that, in fact, a lot of app server will have the corresponding configuration place, but the large server just provide the admin Console, above can be explicitly configured connection Pool, there are also obvious attribute selection. • It is worth mentioning that removeabandoned: is self-interrupting, the default is False removeabandonedtimeout: After a few seconds of self-interruption, removeabandoned must be true LogaBandoned: If an interrupt event is logged, the default is False for these three properties.
Reprint Tomcat Connection Pool Configuration