首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

tomcat配备 oracle 连接池

2012-10-31 
tomcat配置 oracle 连接池solarcn debug5 reloadabletrue crossContexttrue???Resource name

tomcat配置 oracle 连接池
solarcn" debug="5" reloadable="true" crossContext="true">
??? <Resource name="solar_ds"
???? auth="Container"
???? type="javax.sql.DataSource"
???? driverClassName="oracle.jdbc.OracleDriver"
???? url="jdbc:oracle:thin:@192.168.0.25:1521:ora"
???? username="solarcn"
???? password="solarcn"
???? maxActive="5"
???? maxIdle="10"
???? maxWait="5000"/>
??? </Context>
<!--//pool-->

--红色部分是需要你怎么更改的,现在挨个来解释下:

1.path="/solarcn" :这个是你的工程名的访问路径;

2.docBase="solarcn":工程名;

3.Resource name="solar_ds" :数据源的名;

4.url="jdbc:oracle:thin:@192.168.0.25:1521:ora":数据库连接路径;

下面的数据库用户名密码就不用说了....

其次,在%tomcat_root%/conf/web.xml中引用数据源;

在最后的</webapp>前加入以下代码:

<resource-ref>
?? <description>Oracle Datasource example</description>
?? <res-ref-name>solar_ds</res-ref-name>
?? <res-type>javax.sql.DataSource</res-type>
?? <res-auth>Container</res-auth>
</resource-ref>

红色部分就是数据源的名称,和上面server.xml中的 Resource name="solar_ds" 相对应;

然后就是在程序中获得数据源了;

public static Connection getConn(){
?? Connection conn = null;
?? DataSource ds = null;
?? try {
??? Context ctx = new InitialContext();
??? Context envCtx = (Context) ctx.lookup("java:comp/env");
??? ds =(DataSource)envCtx.lookup("solar_ds");??? //获得数据源,红色名字与上面server.xml中的数据源名字对应
??? conn = ds.getConnection();
?? } catch (NamingException e) {
??? e.printStackTrace();
?? }catch (SQLException e) {
??? e.printStackTrace();
?? }??
?? return conn;
}

这样就获得了Connection...

这里要注意一点,数据源的名字要统一了,我在server.xml、web.xml和程序中都是用的"solar_ds"作为我的数据源名称;

=================================================================================

下面说一下在配置过程中遇到的几个比较棘手的问题,这几个问题是在配置连接池时的主要拦路虎,我百度了大半天,终于一一搞定了````tomcat配备 oracle 连接池...

1.javax.naming.NameNotFoundException: Name java:comp is not bound in this Context

--这是在初始化context时(Context envCtx = (Context) ctx.lookup("java:comp/env");)抛出的异常;解决方法是把工程/WEB-INF/lib下面naming-common.jar,naming-factory.jar,naming-resources.jar三个jar包删除;

2。org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (Io 异常: Connection refused(DESCRIPTION=(TMP=)(VSNNUM=150999297)(ERR=12505)(ERROR_STACK=(ERROR=(CODE=12505)(EMFI=4)))))

--这是在获得Connection的时候抛出的异常,前提是已经获得了数据源DataSource;出现这种情况是无法创建连接池工厂对象,原因是连接被拒绝。

到此,应该就会成功的配置好连接池了,可以自己建个jsp测试一下拉...

热点排行