ibatis+jndi+transactionManager+DataSource

首先,写一个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值