数据库连接池配置?mysql的数据连接池怎么配置文件
大家好,如果您还对数据库连接池配置不太了解,没有关系,今天就由本站为大家分享数据库连接池配置的知识,包括mysql的数据连接池怎么配置文件的问题都会给大家分析到,还望可以解决大家的问题,下面我们就开始吧!
java数据库连接池配置的几种方法
数据库连接池的主要操作如下:
(1)建立数据库连接池对象(服务器启动)。
(2)按照事先指定的参数创建初始数量的数据库连接(即:空闲连接数)。
(3)对于一个数据库访问请求,直接从连接池中得到一个连接。如果数据库连接池对象中没有空闲的连接,且连接数没有达到最大(即:最大活跃连接数),创建一个新的数据库连接。
(4)存取数据库。
(5)关闭数据库,释放所有数据库连接(此时的关闭数据库连接,并非真正关闭,而是将其放入空闲队列中。如实际空闲连接数大于初始空闲连接数则释放连接)。
(6)释放数据库连接池对象(服务器停止、维护期间,释放数据库连接池对象,并释放所有连接)。
TOMCAT怎么配置连接池
Tomcat的连接池配置方式具体如下:
一.配置tomcat连接池
1.修改server.xml或者在conf/Catalina/localhost下添加对应的xml片段。此处使用的是第二种方法。
在conf/Catalina/localhost文件夹下新建ROOT.xml片段,添加以下内容:
<?xmlversion="1.0"encoding="UTF-8"?>
<Context crossContext="true" useHttpOnly="true">
<Resourcename="jdbc/infogrid" type="javax.sql.DataSource" password="123456" driverClassName="com.mysql.jdbc.Driver" maxIdle="10" maxWait="50" username="root" url="jdbc:mysql://localhost:3306/infogrid?autoReconnect=true" maxActive="20"/>
</Context>
2.为tomcat添加数据库连接驱动包,本用例使用mysql,将mysql的驱动包添加到common/lib下
二.编写java测试类(该类必须部署在tomcat容器中,否则无法获得JNDI资源)
PreparedStatement ps=null;
ResultSet rs=null;
try{
InitialContext ctx=new InitialContext();
DataSource dataSource=(DataSource)ctx.lookup("java:comp/env/jdbc/infogrid");
Connection connection=dataSource.getConnection();
ps=connection.prepareStatement("select count(*) from test");
rs=ps.executeQuery();
rs.next();
System.out.println("表中行数为:"+rs.getInt(1));
rs.close();
ps.close();
System.out.println(connection.getClass());
//class org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper
Field field=connection.getClass().getDeclaredField("delegate");
field.setAccessible(true);
System.out.println(field.get(connection).getClass());
//class org.apache.tomcat.dbcp.dbcp.PoolableConnection
connection.close();
//此关闭并非为真得关闭数据库连接,具体该close实现可见上面的两个Connection代理类,其实只是告诉连接池,该Connection已经用完了,可以被再次利用了
}catch(Exception ex){
ex.printStackTrace();
}
mysql的数据连接池怎么配置文件
mysql的数据连接池怎么配置文件
连接先建立一些连接,并且这些连接允许共享,因此这样就节省了每次连接的时间开销。Mysql数据库为例,连接池在Tomcat中的配置与使用。
1、创建数据库Student,表student
2、配置server.xml文件。Tomcat安装目录下conf中server.xml文件。
<GlobalNamingResources>
<Resource
name="jdbc/DBPool"
type="javax.sql.DataSource"
password=""
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost:3306/student"
maxActive="3"
/>
</GlobalNamingResources>
name:指定连接池的名称
type:指定连接池的类,他负责连接池的事务处理
url:指定要连接的数据库
driverClassName:指定连接数据库使用的驱动程序
username:数据库用户名
password:数据库密码
maxWait:指定最大建立连接等待时间,如果超过此时间将接到异常
maxIdle:指定连接池中连接的最大空闲数
maxActive:指定连接池最大连接数
3、配置web.xml文件。
<web-app>
<resource-ref>
<description>mysql数据库连接池配置</description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
4、配置context.xml文件
与server.xml文件所在的位置相同。
<Context>
<ResourceLink
name="jdbc/DBPool"
type="javax.sql.DataSource"
global="jdbc/DBPool"
/>
</Context>
5、测试
DataSource pool= null;
Context env= null;
Connection conn= null;
Statement st= null;
ResultSet rs= null;
try{
env=(Context)new InitialContext().lookup("java:comp/env");
//检索指定的对象,返回此上下文的一个新实例
pool=(DataSource)env.lookup("jdbc/DBPool");
//获得数据库连接池
if(pool==null){out.printl("找不到指定的连接池!");}
con= pool.getConnection();
st= con.createStatement();
rs= st.executeQuery("select* from student");
}catch(Exception ex){out.printl(ne.toString());}
Java数据库连接池的几种配置方法(以MySQL数
连接先建立一些连接,并且这些连接允许共享,因此这样就节省了每次连接的时间开销。Mysql数据库为例,连接池在Tomcat中的配置与使用。
1、创建数据库Student,表student
2、配置server.xml文件。Tomcat安装目录下conf中server.xml文件。
<GlobalNamingResources>
<Resource
name="jdbc/DBPool"
type="javax.sql.DataSource"
password=""
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost:3306/student"
maxActive="3"
/>
</GlobalNamingResources>
name:指定连接池的名称
type:指定连接池的类,他负责连接池的事务处理
url:指定要连接的数据库
driverClassName:指定连接数据库使用的驱动程序
username:数据库用户名
password:数据库密码
maxWait:指定最大建立连接等待时间,如果超过此时间将接到异常
maxIdle:指定连接池中连接的最大空闲数
maxActive:指定连接池最大连接数
3、配置web.xml文件。
<web-app>
<resource-ref>
<description>mysql数据库连接池配置</description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
4、配置context.xml文件
与server.xml文件所在的位置相同。
<Context>
<ResourceLink
name="jdbc/DBPool"
type="javax.sql.DataSource"
global="jdbc/DBPool"
/>
</Context>
5、测试
DataSource pool= null;
Context env= null;
Connection conn= null;
Statement st= null;
ResultSet rs= null;
try{
env=(Context)new InitialContext().lookup("java:comp/env");
//检索指定的对象,返回此上下文的一个新实例
pool=(DataSource)env.lookup("jdbc/DBPool");
//获得数据库连接池
if(pool==null){out.printl("找不到指定的连接池!");}
con= pool.getConnection();
st= con.createStatement();
rs= st.executeQuery("select* from student");
}catch(Exception ex){out.printl(ne.toString());}
OK,关于数据库连接池配置和mysql的数据连接池怎么配置文件的内容到此结束了,希望对大家有所帮助。