连接池技术
1.配置数据源路径
配置到Tomcat安装目录下的conf\server.xml文件中。也可以将其配置到web工程目录下的META-INF\context.xml文件中。建议采用后者。
2.配置数据源
<Context><Resource name="TestJNDI"
type="javax.sql.DataSource"
auth="Container"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/shen"
username="root"
password="123456"
maxActive="4"
maxIdle="2"
maxWait="6000"/>
</Context>
3.对上面的属性的描述
a) 设置数据源的JNDI名:name="TestJNDI";b) 设置数据源的类型:type="javax.sql.DataSource"
c) 设置数据源的管理者:auth="Container"
d) 数据库的驱动:driverClassName="com.mysql.jdbc.Driver"
e) 数据库的路径:url="jdbc:mysql://localhost:3306/shen"
f) 数据库的用户名:username="root"
g) 数据库的密码:password="123456"
h) 活动状态的连接最大数目:maxActive="4"
i) 空闲最大数目(0表示不限制):maxIdle="2"
j) 连接池中没有空闲的,我们最多等待时间是:(-1表示无限等待,单位毫秒):maxWait="6000"
4.应用连接池
static private final String JNDI="TestJNDI";static DataSource ds;
static{
try{
Context ctx = new InitialContext();
ctx = (Context)ctx.lookup("java:comp/env");
ds = (DataSource)ctx.looup(JNDI);
}catch(NamingException e){}
}