【Java】数据库连接池的简单使用
一、本地数据库连接池使用
1、 配置全局(在本地 tomcat 配置)
a. 打开 tomcat/conf/context.xml 文件添加如下配置
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!-- JNDI tomcat 数据库连接池配置 --> <Resource name= "db" auth= "Container" maxActive= "100" maxIdle= "30" maxWait= "10000" type= "javax.sql.DataSource" username= "root" password= "root" driverClassName= "com.mysql.cj.jdbc.Driver" url= "jdbc:mysql://localhost:3306/db?serverTimezone=PRC" /> |
属性名称 |
说明 |
name |
指定 Resource 的 JNDI 名称 |
auth |
指定管理 Resource 的 Manager(Container: 由容器创建和管理 |Application:由 Web 应用创建和管理) |
type |
指定 Resource 所属的 Java 类 javax.sql.DataSource |
maxActive |
指定连接池中处于活动状态的数据库连接的最大数目 |
maxIdle |
指定连接池中处于空闲状态的数据库连接的最大数目 |
maxWait |
指定连接池中的连接处于空闲的最长时间,超过这个时间会抛出异常,取值为 -1,表示可以无限期等待 |
b. 使用数据源,在 web 中测试(即获取 context.xml 文件配置)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <%@ page import = "javax.naming.Context" %> <%@ page import = "javax.naming.InitialContext" %> <%@ page import = "javax.sql.DataSource" %> <%@ page import = "java.sql.Connection" %> <%@ page import = "java.sql.Statement" %> <%@ page import = "java.sql.ResultSet" %> <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>title</title> </head> <body> <% Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup( "java:comp/env/db" ); Connection conn = ds.getConnection(); out.print(conn); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery( "select version()" ); rs.next(); out.print(rs.getString( 1 )); rs.close(); stmt.close(); %> </body> </html> |
2、 在 web 项目中使用(不使用全局)
a. 首先注释写在 tomcat/conf/context.xml 数据库连接池配置,在 web 项目下的 webapp 目录下初建一个 META-INF 目录,然后再在 META-INF 目录下创建一个 context.xml 文件,写入如下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?xml version= "1.0" encoding= "UTF-8" ?> <Context> <!-- 默认的连接池 --> <Resource name= "db" auth= "Container" maxActive= "100" maxIdle= "30" maxWait= "10000" type= "javax.sql.DataSource" username= "root" password= "root" driverClassName= "com.mysql.cj.jdbc.Driver" url= "jdbc:mysql://localhost:3306/db?serverTimezone=PRC" /> </Context> |
b. 继续用之前的 web 代码测试即可
二、阿里数据库连接池使用(和上面的使用几乎一样,只有连接池的配置发生改变)
a. 在 web 项目下的 META-INF/context.xml 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?xml version= "1.0" encoding= "UTF-8" ?> <Context> <!-- 默认的连接池 --> <Resource name= "db" auth= "Container" maxActive= "100" maxIdle= "30" maxWait= "10000" type= "javax.sql.DataSource" username= "root" password= "root" driverClassName= "com.mysql.cj.jdbc.Driver" url= "jdbc:mysql://localhost:3306/db?serverTimezone=PRC" /> <!-- 阿里连接池 --> <Resource name= "druid" auth= "Container" factory= "com.alibaba.druid.pool.DruidDataSourceFactory" type= "javax.sql.DataSource" username= "root" password= "root" driverClassName= "com.mysql.cj.jdbc.Driver" url= "jdbc:mysql://localhost:3306/db?serverTimezone=PRC" filters= "stat" /> </Context> |
b. 也可以使用全局配置,同样也是在 tomcat/conf/context.xml 配置
c. 连接池测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <%@ page import = "javax.naming.Context" %> <%@ page import = "javax.naming.InitialContext" %> <%@ page import = "javax.sql.DataSource" %> <%@ page import = "java.sql.Connection" %> <%@ page import = "java.sql.Statement" %> <%@ page import = "java.sql.ResultSet" %> <%@ page contentType= "text/html;charset=UTF-8" language= "java" %> <html> <head> <title>title</title> </head> <body> <% Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup( "java:comp/env/druid" ); Connection conn = ds.getConnection(); out.print(conn); %> </body> </html> |
三、Druid 监控统计
a. 只需在 WEB-INF/web.xml 添加配置即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <!-- Druid监控统计 --> <servlet> <servlet-name>druidr</servlet-name> <servlet- class >com.alibaba.druid.support.http.StatViewServlet</servlet- class > <!-- <init-param>--> <!-- <!– 是否清空统计数据 –>--> <!-- <param-name>resetEnable</param-name>--> <!-- <param-value> true </param-value>--> <!-- </init-param>--> <init-param> <!-- 用户名 --> <param-name>loginUsername</param-name> <param-value>admin</param-value> </init-param> <init-param> <!-- 密码 --> <param-name>loginPassword</param-name> <param-value>admin</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>druidr</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> |
b. 当在网页中执行数据库操作时都可以查看到,因为我使用的是本机、端口为 8080、tomcat 根目录为空,我直接输入 loaclhost:8080/druid,即可看到 druid 统计监控,输入先前设好的账号密码即可,效果如下