文中內(nèi)容主要轉(zhuǎn)自: http://www.open-open.com/lib/view/open1327478028639.html ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? http://www.open-open.com/lib/view/open1365991769687.html ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? http://blog.csdn.net/lzm1340458776/article/details/37901619
tomcat jdbc pool 可在 Tomcat 中直接使用,也可以在獨立的應(yīng)用中使用。
?
屬性說明:
name | 數(shù)據(jù)源名稱,這個隨便,通常取為jdbc/XXX的格式 |
auth | Container容器 |
type | javax.sql.DataSource 注意是javax不是java |
username | 數(shù)據(jù)庫用戶名 |
password | 數(shù)據(jù)庫用戶密碼 |
maxIdle | 最大空閑數(shù),數(shù)據(jù)庫連接的最大空閑時間。超過空閑時間,數(shù)據(jù)庫連接將被標記為不可用,然后被釋放。設(shè)為0表示無限制。 |
maxActive | 連接池的最大數(shù)據(jù)庫連接數(shù)。設(shè)為0表示無限制。 |
maxWait | 等待連接的最大連接的時間。 |
driverClassName | 數(shù)據(jù)庫完整的驅(qū)動類全稱。 |
url | 數(shù)據(jù)庫鏈接 |
?
?
?
?
?
?
?
?
?
?
1.?在獨立應(yīng)用中使用:
1
import
java.sql.Connection;
2
import
java.sql.ResultSet;
3
import
java.sql.Statement;
4
5
import
org.apache.tomcat.jdbc.pool.DataSource;
6
import
org.apache.tomcat.jdbc.pool.PoolProperties;
7
8
public
class
SimplePOJOExample {
9
10
public
static
void
main(String[] args)
throws
Exception {
11
PoolProperties p = new PoolProperties();
12
p.setUrl("jdbc:mysql://localhost:3306/mysql"
);
13
p.setDriverClassName("com.mysql.jdbc.Driver"
);
14
p.setUsername("root"
);
15
p.setPassword("password"
);
16
p.setJmxEnabled(
true
);
17
p.setTestWhileIdle(
false
);
18
p.setTestOnBorrow(
true
);
19
p.setValidationQuery("SELECT 1"
);
20
p.setTestOnReturn(
false
);
21
p.setValidationInterval(30000
);
22
p.setTimeBetweenEvictionRunsMillis(30000
);
23
p.setMaxActive(100
);
24
p.setInitialSize(10
);
25
p.setMaxWait(10000
);
26
p.setRemoveAbandonedTimeout(60
);
27
p.setMinEvictableIdleTimeMillis(30000
);
28
p.setMinIdle(10
);
29
p.setLogAbandoned(
true
);
30
p.setRemoveAbandoned(
true
);
31
p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
32
"org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
);
33
DataSource datasource =
new
DataSource();
34
datasource.setPoolProperties(p);
35
36
Connection con =
null
;
37
try
{
38
con =
datasource.getConnection();
39
Statement st =
con.createStatement();
40
ResultSet rs = st.executeQuery("select * from user"
);
41
int
cnt = 1
;
42
while
(rs.next()) {
43
System.out.println((cnt++)+". Host:" +rs.getString("Host")+
44
" User:"+rs.getString("User")+" Password:"+rs.getString("Password"
));
45
}
46
rs.close();
47
st.close();
48
}
finally
{
49
if
(con!=
null
)
try
{con.close();}
catch
(Exception ignore) {}
50
}
51
}
52
53
}
?
2.? 在 Tomcat 中直接使用
? ? 方式一、
? ? 1). 找到tomcat所在目錄中的conf文件夾中的context.xml文件,在其中做修改(在Context結(jié)點里面添加):? ? ? ? ?
1
<!—mysql數(shù)據(jù)源配置-->
2
<
Resource
3
name="ds_mysql"
4
auth="Container"
5
type="javax.sql.DataSource"
6
maxActive="100"
7
maxIdel="30"
8
maxWait="10000"
9
username="root"
10
password="5982285"
11
driverClassName="com.mysql.jdbc.Driver"
12
url="jdbc:mysql://localhost/myblog"
13
/>
14
<!—oracle數(shù)據(jù)源配置-->
15
<
Resource
16
name="jdbc/oracleds"
17
auth="Container"
18
type="javax.sql.DataSource"
19
maxActive="100"
20
maxIdel="30"
21
maxWait="10000"
22
username="scott"
23
password="tiger"
24
driverClassName="oracle.jdbc.driver.OracleDriver"
25
url="jdbc:oracle:thin:@localhost:1521:sky"
26
/>
?
注:上面為mysql和Oracle配置了數(shù)據(jù)源,看個人需求而定,二者選其一。
注:網(wǎng)上很多人都說需要在web.xml文件中再次配置,其實不配置也是可以的(本人就沒有在web.xml配置,依然跑的很溜),另外有一個要
? ? ? ?特別注意的是要將數(shù)據(jù)源 的jar包和數(shù)據(jù)庫的驅(qū)動包放到tomcat的lib包中。
?
JNDI技術(shù)簡介
JNDI(Java Naming and Directory Interface)即Java命名和目錄接口,它對應(yīng)于Java EE中的javax.naming包,這套API的主要作用:它可以
把DataSource對象放在一個 Tomcat容器中(JNDI容器),并為容器中的DataSource對象取一個名稱,以后程序想獲得DataSource對象,只需
要通過名稱檢索即可。
JNDI的核心API為Context,它代表JNDI容器,核心方法是lookup()它的功能是檢索容器中對應(yīng)名稱的對象。
?
連接池工具類:
1
/**
2
* 獲取數(shù)據(jù)庫連接的工廠
3
*
@author
Liao
4
*/
5
public
class
ConnectionFactory {
6
7
private
static
DataSource dataSource;
8
9
static
{
10
try
{
11
//
初始化查找命名空間
12
Context context =
new
InitialContext();
13
//
找到DataSource。?
java:/comp/env為固定路徑。 ?ds_mysql是tomcat中設(shè)置的數(shù)據(jù)源
?
14
dataSource = (DataSource) context.lookup("java:comp/env/ds_mysql"
);
15
}
catch
(Exception e) {
16
e.printStackTrace();
17
}
18
}
19
20
/**
21
* 獲取數(shù)據(jù)庫連接
22
*/
23
public
static
Connection getConnection(){
24
try
{
25
//
通過數(shù)據(jù)源獲取連接然后返回
26
return
dataSource.getConnection();
27
}
catch
(Exception e) {
28
e.printStackTrace();
29
return
null
;
30
}
31
}
32
33
/**
34
* 關(guān)閉資源
35
*
@param
conn
36
*
@param
sta
37
*
@param
res
38
*/
39
public
static
void
closeConnection(Statement sta, ResultSet res) {
40
try
{
41
if
(res !=
null
) {
42
res.close();
43
res =
null
;
44
}
45
if
(sta !=
null
) {
46
sta.close();
47
sta =
null
;
48
}
49
50
}
catch
(SQLException e) {
51
e.printStackTrace();
52
}
53
}
54
}
注:上述程序,沒有關(guān)閉連接,是因為用完連接之后,連接又回到了連接池,處于空閑狀態(tài)。 ? ? ? ?
java:/comp/env為固定路徑。 ?ds_mysql是tomcat中設(shè)置的數(shù)據(jù)源
?
連接池配置常見錯誤:
Invalid byte 1 of 1-byte UTF-8 sequence.
原因:
在context.xml文件中使用了中文的注釋(這個確實比較坑爹,但確實是這樣的)。
解決方案:
把中文的注釋去掉即可。 ?
?
方式二、
? ? 1). 在conf/server.xml下的<GlobalNamingResources>節(jié)點里配置resource,例如:? ? ? ? ?
1
<Resource name="jdbc/ens"
2
auth="Container"
3
type="javax.sql.DataSource"
4
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
5
testWhileIdle="true"
6
testOnBorrow="true"
7
testOnReturn="false"
8
validationInterval="30000"
9
timeBetweenEvictionRunsMillis="30000"
10
maxActive="100"
11
minIdle="10"
12
maxWait="10000"
13
initialSize="10"
14
removeAbandonedTimeout="60"
15
removeAbandoned="true"
16
logAbandoned="true"
17
minEvictableIdleTimeMillis="30000"
18
jmxEnabled="true"
19
jdbcInterceptors=
20
"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
21
username="root"
22
password="123"
23
driverClassName="com.mysql.jdbc.Driver"
24
url="jdbc:mysql://localhost:3306/ens"/>
然后,在context.xml文件的<Context></Context>節(jié)點中添加如下配置:
1
<ResourceLink global="jdbc/ens" name="jdbc/ens" type="javax.sql.DataSource"/>
global="jdbc/ens" 中的參數(shù)值("jdbc/ens")必須和上一段<Resource >配置中的name屬性的值保持一樣。name="jdbc/ens" 這個可以隨便取,
但是在程序中調(diào)用的時候, 就應(yīng)該與name的值保持一致。到這里,連接池已經(jīng)配置好啦。 用JSP測試一下:
1
<%@ page language="java" pageEncoding="gbk"%>
2
<%@page
import
="java.sql.Connection"%>
3
<%@page
import
="javax.naming.Context"%>
4
<%@page
import
="javax.naming.InitialContext"%>
5
<%@page
import
="javax.sql.DataSource"%>
6
<%@page
import
="java.sql.Statement"%>
7
<%@page
import
="java.sql.ResultSet"%>
8
<%
9
//
連接池的獲取
10
Connection conn =
null
;
11
DataSource ds =
null
;
12
ResultSet rs =
null
;
13
Statement stmt =
null
;
14
Context initCtx =
new
InitialContext();
15
ds =(DataSource)initCtx.lookup("java:comp/env/jdbc/ens"
);
16
if
(ds!=
null
){
17
out.println("已經(jīng)獲得DataSource!"
);
18
out.println("<br>"
);
19
conn =
ds.getConnection();
20
try
{
21
stmt =
conn.createStatement();
22
String sql ="select * from ens_area"
;
23
rs =
stmt.executeQuery(sql);
24
out.println("以下是從數(shù)據(jù)庫中讀取出來的數(shù)據(jù):<br>"
);
25
while
(rs.next()){
26
out.println("<br>"
);
27
out.println(rs.getString("area_name"
));
28
}
29
}
catch
(Exception ex){
30
ex.printStackTrace();
31
}
finally
{
32
conn.close();
33
rs.close();
34
stmt.close();
35
}
36
}
37
%>
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
