Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Database connection Pool setup and Application of Tomcat (Mysql)

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. Put the JAR file of the database driver in the common/lib of Tomcat.

Download address: http://dev.mysql.com/downloads/

2. Set the data source in server.xml. Take the MySQL database as an example, as follows:

Add to the node

Name= "jdbc/DBPool"

Type= "javax.sql.DataSource"

Password= "xxxxxxxx" my password is xxxxxxxx

DriverClassName= "com.mysql.jdbc.Driver"

MaxIdle= "2"

MaxWait= "5000"

Username= "root"

Url= "jdbc:mysql://127.0.0.1:3306/test"

MaxActive= "4" / >

Attribute description: name, data source name, usually in the format of "jdbc/XXX"; the name must be interesting. Easy to remember

Type, "javax.sql.DataSource"

Password, database user password

DriveClassName, database driven

MaxIdle, maximum number of idle, maximum idle time of database connection. Over idle time, database connection

It will then be marked as unavailable and then released. Set to 0 means there is no limit.

MaxActive, the maximum number of database connections for the connection pool. Set to 0 means there is no limit.

MaxWait, the maximum waiting time for establishing a connection. If you exceed this time, you will receive an exception. Set to-1 means

There is no limit.

3. Set the data source reference in the web.xml of your web application, as follows:

Add to the node

MySQL DB Connection Pool

Jdbc/DBPool

Javax.sql.DataSource

Container

Shareable

Child node description: description, description information

Res-ref-name, the name of the reference data source, and the attribute name of the previous step

Res-type, resource type, "javax.sql.DataSource"

Res-auth, "Container"

Res-sharing-scope, "Shareable"

4. Set the data source link in the context.xml of the web application as follows:

Add to the node

Name= "jdbc/DBPool"

Type= "javax.sql.DataSource"

Global= "jdbc/DBPool" / >

Attribute description: name, same as the attribute name value in steps 2 and 3, and the child node res-ref-name value

Type, also take "javax.sql.DataSource"

Global, the same as the name value.

Now that the setup is complete, here's how to use database connection pooling.

5. Create a connection pool class, DBPool.java, to create a connection pool, as follows:

Import javax.naming.Context

Import javax.naming.InitialContext

Import javax.naming.NamingException

Import javax.sql.DataSource

Public class DBPool {

Private static DataSource pool

Static {

Context env = null

Try {

Env = (Context) new InitialContext () .lookup ("java:comp/env")

Pool = (DataSource) env.lookup ("jdbc/DBPool")

If (pool==null)

System.err.println ("'DBPool' is an unknown DataSource")

} catch (NamingException e) {

E.printStackTrace ()

}

}

Public static DataSource getPool () {

Return pool

}

}

[@ more@]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report