Several configuration methods of Java database connection pool (taking MySQL database as an example)

I Tomcat configuration data source:

Premise: you need to put the jar package connecting the MySQL database driver into the Lib directory under the common folder in the Tomcat installation directory

1. Method 1: create a folder meta inf under Webroot and a file context XML, as follows:

Method 2: create a context under the Tomcat directory conf In XML, modify the original context tag to:

Method 3: when configuring the virtual directory, that is, configure the server under conf XML, in the context tag, change to:

< / the meaning of each attribute in the resource tag in the configuration file:

Driverclassname - the full name of the database driver class used by JDBC Maxactive - the maximum number of active connections provided by the connection pool at the same time. Maxidle - the maximum number of connections maintained by the connection pool at idle time Maxwait - the maximum number of milliseconds the database waits when an exception occurs (when no connection is available) Username - login name when connecting to the database password - password for connecting to the database URL - the URL to connect to the driver (DriverName is also allowed for backward compatibility.)

Test code:

Printing out is not null and should succeed.

Note that when testing, you should test in tomcat, that is, in the Tomcat container (don't bother, write a simple JSP page and use <%...% >, which is quite simple). If you don't test in the Tomcat container, exceptions will be thrown:

... javax. naming. NoInitialContextException: Need to specify class name in environment or system property,or as an applet parameter,or in an application resource file: java. naming. factory. initial

2、 Hibernate has three methods to configure connection pool:

Method 1 uses hibernate's own connection pool.

Method 2: use the database connection pool specified in the configuration file.

The connection pool now has DBCP, c3p0 and proxoop. In fact, I knew DBCP

The configuration of DBCP and c3p0 only needs to add some configuration to the above configuration, and Hibernate will automatically identify the database connection pool

To configure DBCP, you need to add:

Configuration c3p0 needs to include:

The configuration of proxoop is somewhat different. You can't just add it, but you need to change it:

The basic configuration is as follows:

Special note: the path of the following files should be configured correctly, otherwise filenotfound

Associated file: Test / huatech / conf / proxoolconf The XML configuration is as follows:

Method 3: get the connection pool from the container (e.g. Tomcat)

Use the connection pool of the server itself, such as tomcat, resin, Weblogic, etc

JDBC / CRM of Java: comp / env / JDBC / CRM is the name of the database connection pool in the corresponding server, which needs to be configured in the corresponding environment

Tomcat configuration is described in the first Tomcat configuration method. Note that the name of JNDI should be modified according to the situation and should correspond to the name used by hibernate======================================== The above configuration requires the jar package of the respective database connection pool, which is available in the hibernate package. If you need the latest one, you can download it from their respective websites.

3、 How spring configures the connection pool:

4、 The last one I want to talk about today is to configure the connection pool through code writing. The code is as follows:

To get the connection pool, you can only use the static method setupdatasource () of this class

The above are several configuration methods of Java database connection pool introduced by Xiaobian (taking MySQL database as an example). I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>