在Tomcat上配置Proxool的DataSource(Jndi) --第2种方法

Posted on 2009-02-17 14:15 太清剑客 阅读(3873) 评论(0)  编辑  收藏 所属分类: JAVA

关于第一个方法请看
http://www.blogjava.net/kreo/archive/2009/02/12/254348.html


第一个方法是在容器,也就是Tomcat上做配置,但是,有时候会不适用,比如虚拟主机,它可能不允许你在机器上做配置,那样,就只能从自身的程序着手.

首先,写一个Proxool的配置文件.....放在 WEB-INF 目录下,下面是一个例子
<?xml version="1.0" encoding="UTF-8"?>
    
<!--
        the proxool configuration can be embedded within your own application's. Anything outside the "proxool" tag is
        ignored.
    
-->
<something-else-entirely>
    
<proxool>
        
<alias>proxool_alias</alias>
        
<driver-url>jdbc:oracle:thin:@127.0.0.1:1521:sid</driver-url>
        
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
        
<driver-properties>
            
<property name="user" value="user" />
            
<property name="password" value="pwd" />
        
</driver-properties>
        
<maximum-connection-count>10</maximum-connection-count>
        
<minimum-connection-count>5</minimum-connection-count>
        
<maximum-connection-lifetime>30</maximum-connection-lifetime>
        
<maximum-active-time>3</maximum-active-time>
        
<simultaneous-build-throttle>5</simultaneous-build-throttle>
        
<house-keeping-sleep-time>30000</house-keeping-sleep-time>
        
<house-keeping-test-sql>Select Sysdate Form dual</house-keeping-test-sql>
        
<prototype-count>0</prototype-count>
    
</proxool>
</something-else-entirely>

然后,当然是先把这个文件先用上,看看可不可以用

在web.xml里面配置上
    <servlet>
        
<servlet-name>ServletConfigurator</servlet-name>
        
<servlet-class>org.logicalcobwebs.proxool.configuration.ServletConfigurator</servlet-class>
        
<init-param>
            
<param-name>xmlFile</param-name>
            
<param-value>WEB-INF/proxool.xml</param-value>
        
</init-param>
        
<load-on-startup>1</load-on-startup>
    
</servlet>
    
<servlet>
        
<servlet-name>proxooladmin</servlet-name>
        
<servlet-class> org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
    
</servlet>
    
<servlet-mapping>
        
<servlet-name>proxooladmin</servlet-name>
        
<url-pattern>/proxooladmin.servlet</url-pattern>
    
</servlet-mapping>

这样在应用启动之后,就能通过以下代码来访问到数据库

    Connection conn = DriverManager.getConnection("proxool.proxool_alias");
    Statement stmt 
= conn.createStatement();
    ResultSet rs 
= stmt.executeQuery("Select * From test");

/*以下是重点*/
但是我们需要的是注册一个JNDI,
有2种方法
1.配置文档里面直接注册,这样配置:
<?xml version="1.0" encoding="UTF-8"?>
    
<!--
        the proxool configuration can be embedded within your own application's. Anything outside the "proxool" tag is
        ignored.
    
-->
<something-else-entirely>
    
<proxool>
        
<alias>proxool_alias</alias>
        
<driver-url>jdbc:oracle:thin:@127.0.0.1:1521:pub</driver-url>
        
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
        
<driver-properties>
            
<property name="user" value="user" />
            
<property name="password" value="pwd" />
        
</driver-properties>
        
<jndi-name>jdbc-0</jndi-name>
        
<jndi-java.naming.factory.initial>org.apache.naming.java.javaURLContextFactory</jndi-java.naming.factory.initial>
        
<jndi-java.naming.factory.url.pkgs>org.apache.naming</jndi-java.naming.factory.url.pkgs>
        
<maximum-connection-count>10</maximum-connection-count>
        
<minimum-connection-count>5</minimum-connection-count>
        
<maximum-connection-lifetime>30</maximum-connection-lifetime>
        
<maximum-active-time>3</maximum-active-time>
        
<simultaneous-build-throttle>5</simultaneous-build-throttle>
        
<house-keeping-sleep-time>30000</house-keeping-sleep-time>
        
<house-keeping-test-sql>Select Sysdate Form dual</house-keeping-test-sql>
        
<prototype-count>0</prototype-count>
    
</proxool>
</something-else-entirely>


2.在自己的Java类里面注册JNDI
那就需要用到Proxool提供的另一个类:ProxoolJNDIHelper:
        Properties info = new Properties();
        info.setProperty(
"jndi-name""jdbc-0");
        info.setProperty(
"java.naming.factory.initial", org.apache.naming.java.javaURLContextFactory.class.getName());
        info.setProperty(
"java.naming.factory.url.pkgs""org.apache.naming");
        
// info.setProperty("java.naming.provider.url", "localhost:8888");
     try {
            ProxoolJNDIHelper.registerDatasource(
"proxool_alias", info);
     }
 catch (ProxoolException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
         }

上面所说的2中方法都能注册一个名为jdbc-0的JNDI,
可以通过以下代码来访问数据库..
        Context ctx = new InitialContext();
        DataSource dataSource 
= (DataSource) ctx.lookup("jdbc-0");
            
        Connection conn 
= dataSource.getConnection();

下面说明代码,
1.jndi-name好像不能用jdbc/xx这种名字,文档里面用的就是/datasources/UserDB,但是Tomcat启动就报错,可能和特殊字符有关,具体没测试
2.JNDI的几个参数(java.naming.factory.initial,java.naming.factory.url.pkgs,还有java.naming.provider.url),这些参数和容器有关,Tomcat就是这些参数
JBOSS的话就是:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost:1099
java.naming.factory.url.pkgs=org.jboss.naming

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 10, comments - 4, trackbacks - 0, articles - 0

Copyright © 太清剑客