您的位置:首页 > 职场人生

proxool的使用总结

2011-09-26 15:33 274 查看
在项目中使用proxool做数据源管理,这里记录下配置代码与需要注意的事项。

添加jar包:proxool-0.9.0RC3.jar在WEB-INF下,创建proxool.xml文件,代码如下:

01
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
<!--theproxoolconfigurationcanbeembeddedwithinyourownapplication's.
03
Anythingoutsidethe"proxool"tagisignored.-->
04
<
something-else-entirely
>
05
<
proxool
>
06
<
alias
>数据源名</
alias
>
07
<
driver-url
>jdbc:mysql://localhost:3306/数据库名</
driver-url
>
08
<
driver-class
>com.mysql.jdbc.Driver</
driver-class
>
09
<
driver-properties
>
10
<
property
name
=
"user"
value
=
"用户名"
/>
11
<
property
name
=
"password"
value
=
"密码"
/>
12
<
property
name
=
"useUnicode"
value
=
"true"
/>
13
<
property
name
=
"characterEncoding"
value
=
"UTF-8"
/>
14
</
driver-properties
>
15
16
<
house-keeping-sleep-time
>40000</
house-keeping-sleep-time
>
17
<
maximum-connection-count
>250</
maximum-connection-count
>
18
<
minimum-connection-count
>3</
minimum-connection-count
>
19
<
maximum-connection-lifetime
>3000000</
maximum-connection-lifetime
>
<!--5hours-->
20
<
simultaneous-build-throttle
>5</
simultaneous-build-throttle
>
21
<
recently-started-threshold
>400000</
recently-started-threshold
>
22
<
overload-without-refusal-lifetime
>500000</
overload-without-refusal-lifetime
>
23
<
maximum-active-time
>600000</
maximum-active-time
>
24
<
verbose
>true</
verbose
>
25
<
trace
>true</
trace
>
26
<
house-keeping-test-sql
>selectCURRENT_DATE</
house-keeping-test-sql
>
27
<
fatal-sql-exception
>Fatalerror</
fatal-sql-exception
>
28
<
prototype-count
>2</
prototype-count
>
29
<
statistics-log-level
>INFO</
statistics-log-level
>
30
</
proxool
>
31
</
something-else-entirely
>
:proxool.xml的配置参数详细说明请参考这里。

然后在web.xml文件中进行配置,添加如下代码:

01
<
servlet
>
02
<
servlet-name
>ServletConfigurator</
servlet-name
>
03
<
servlet-class
>org.logicalcobwebs.proxool.configuration.ServletConfigurator</
servlet-class
>
04
<
init-param
>
05
<
param-name
>xmlFile</
param-name
>
06
<
param-value
>WEB-INF/proxool.xml</
param-value
>
07
</
init-param
>
08
<
load-on-startup
>1</
load-on-startup
>
09
</
servlet
>
10
<
servlet
>
11
<
servlet-name
>Admin</
servlet-name
>
12
<
servlet-class
>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</
servlet-class
>
13
</
servlet
>-->
14
<!--<servlet-mapping>
15
<servlet-name>Admin</servlet-name>
16
<url-pattern>/admin</url-pattern>
17
</servlet-mapping>-->
上述代码中被注释掉的部分在开发过程中可以释放,如果释放,则会开启数据源监控页面,如果您的访问地址为www.xxx.com,那么数据源监控URL为:www.xxx.com/admin。网站发布到服务器上后,建议关闭该功能。

注意:如果工程中存在监听器,而监听器中调用了数据库中的数据,那么在系统启动的时候会报错

java.sql.SQLException:org.logicalcobwebs.proxool.ProxoolException:Attempttorefertoaunregisteredpoolbyitsalias‘别名’

或者

java.sql.SQLException:Nosuitabledriverfoundfor'数据源名'

这时的解决方案为:把proxool交给监听器进行处理:

新建类:

01
import
java.io.File;
02
import
java.util.Enumeration;
03
import
java.util.Properties;
04
05
import
javax.servlet.ServletContext;
06
import
javax.servlet.ServletContextEvent;
07
import
javax.servlet.ServletContextListener;
08
09
import
org.apache.commons.logging.Log;
10
import
org.apache.commons.logging.LogFactory;
11
import
org.logicalcobwebs.proxool.ProxoolException;
12
import
org.logicalcobwebs.proxool.configuration.JAXPConfigurator;
13
import
org.logicalcobwebs.proxool.configuration.PropertyConfigurator;
14
15
/**
16
*@authorwangtao
17
*/
18
19
public
class
ProxoolListener
implements
ServletContextListener{
20
private
static
final
LogLOG=LogFactory.getLog(ProxoolListener.
class
);
21
22
private
static
final
StringXML_FILE_PROPERTY=
"xmlFile"
;
23
24
private
static
final
StringPROPERTY_FILE_PROPERTY=
"propertyFile"
;
25
26
private
static
final
StringAUTO_SHUTDOWN_PROPERTY=
"autoShutdown"
;
27
28
@SuppressWarnings
(
"unused"
)
29
private
boolean
autoShutdown=
true
;
30
31
public
void
contextDestroyed(ServletContextEventarg0){
32
System.out.println(
"destroydatabasepool...."
);
33
}
34
35
@SuppressWarnings
(
"unchecked"
)
36
public
void
contextInitialized(ServletContextEventcontextEvent){
37
System.out.println(
"init................"
);
38
ServletContextcontext=contextEvent.getServletContext();
//对应servlet的init方法中ServletConfig.getServletContext()
39
StringappDir=contextEvent.getServletContext().getRealPath(
"/"
);
40
Propertiesproperties=
new
Properties();
41
42
Enumerationnames=context.getInitParameterNames();
43
while
(names.hasMoreElements()){
44
Stringname=(String)names.nextElement();
45
Stringvalue=context.getInitParameter(name);
46
47
if
(name.equals(XML_FILE_PROPERTY)){
48
try
{
49
Filefile=
new
File(value);
50
if
(file.isAbsolute()){
51
JAXPConfigurator.configure(value,
false
);
52
}
else
{
53
JAXPConfigurator.configure(appDir+File.separator+value,
false
);
54
}
55
}
catch
(ProxoolExceptione){
56
LOG.error(
"Problemconfiguring"
+value,e);
57
}
58
}
else
if
(name.equals(PROPERTY_FILE_PROPERTY)){
59
try
{
60
Filefile=
new
File(value);
61
if
(file.isAbsolute()){
62
PropertyConfigurator.configure(value);
63
}
else
{
64
PropertyConfigurator.configure(appDir+File.separator+value);
65
}
66
}
catch
(ProxoolExceptione){
67
LOG.error(
"Problemconfiguring"
+value,e);
68
}
69
}
else
if
(name.equals(AUTO_SHUTDOWN_PROPERTY)){
70
autoShutdown=Boolean.valueOf(value).booleanValue();
71
}
else
if
(name.startsWith(
"jdbc"
)){
//此处以前是PropertyConfigurator.PREFIX改为jdbc,因为此源码是0.9.1版本的,与0.9RC3版本有点不一样
72
properties.setProperty(name,value);
73
}
74
}
75
76
if
(properties.size()>
0
){
77
try
{
78
PropertyConfigurator.configure(properties);
79
}
catch
(ProxoolExceptione){
80
LOG.error(
"Problemconfiguringusinginitproperties"
,e);
81
}
82
}
83
}
84
85
}
然后web.xml中的配置如下:

1
<
context-param
>
2
<
param-name
>xmlFile</
param-name
>
3
<
param-value
>WEB-INF/proxool.xml</
param-value
>
4
</
context-param
>
获取数据源连接代码:

viewsourceprint?

01
import
java.sql.Connection;
02
import
java.sql.DriverManager;
03
import
java.sql.SQLException;
04
05
public
class
DBConnectionManager{
06
private
static
Connectionconn;
07
public
static
ConnectiongetConnection()
08
{
09
try
10
{
11
conn=DriverManager.getConnection(
"proxool.数据源名"
);
12
if
(conn==
null
)
13
{
14
throw
new
SQLException(
"数据库无值"
);
15
}
16
17
}
18
catch
(Exceptionex)
19
{
20
System.out.println(ex.getMessage());
21
ex.printStackTrace();
22
}
23
24
return
conn;
25
}
26
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  职场 proxool 休闲