FramworkJava学习笔记程序员

JNDI相关配置

2016-12-21  本文已影响125人  吴世浩

背景

本身已经搭建好了数据源,但是上线的时候老大说需要改为JNDI的配置方式,所以就整了下JNDI的配置,我使用的是阿里巴巴的druid的配置。


JNDI配置问题

一、使用druid进行tomcat配置
修改tomcat下的conf/context.xml文件,在Context节点下配置resource节点
配置如下:

<Resource name="jdbc/test1"  
factory="com.alibaba.druid.pool.DruidDataSourceFactory" 
auth="Container" type="javax.sql.DataSource" 
driverClassName="oracle.jdbc.OracleDriver" 
url="jdbc:oracle:thin:@127.0.0.1:3306:test1"
username="test1" 
password="test1" 
maxActive="100" 
maxWait="-1"
removeabandoned="true"
removeabandonedtimeout="60"
logabandoned="false"
filters="stat" />

<Resource name="jdbc/test2"
factory="com.alibaba.druid.pool.DruidDataSourceFactory"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:3306:test2"
username="test2" 
password="test2" 
maxActive="100" 
maxWait="-1"
removeabandoned="true"
removeabandonedtimeout="60"
logabandoned="false"
filters="stat" />

如果你加了maxIdle =5那么在启动的时候就会报错

严重: maxIdle is deprecated

这是代表现在的jar已经不支持这个属性了,去掉即可。
启动可能包没有jar包,需要将druid的jar复制一份到tomcat下的lib下面

二、相应的spring的配置修改

<!-- 数据库连接 -->
       <bean id="dataSourceMbr" class="org.springframework.jndi.JndiObjectFactoryBean">
              <property name="jndiName">  
            <value>java:comp/env/jdbc/test1</value>  
        </property> 
       </bean>

结合spirng扫描mapper的xml文件并注入数据源

    <bean id="sqlSessionFactoryVoucher" class="org.mybatis.spring.SqlSessionFactoryBean">
              <property name="dataSource" ref="dataSourceVoucher" />
              <!-- 自动扫描mapping.xml文件 -->
              <property name="mapperLocations" value="classpath*:mapper/test1/*.xml"></property>
       </bean>

结合spirng扫描mapper的java文件并

       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
           <property name="basePackage" value="com.mouse.test1.mapper" />
              <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryVoucher"></property>
       </bean>

数据库连接指定多个RAC

   <Resource name="jdbc/mbr" 
              auth="Container" 
              type="javax.sql.DataSource" 
              driverClassName="oracle.jdbc.driver.OracleDriver"
              url="jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (FAILOVER=on) (ADDRESS = 
(PROTOCOL = TCP)(HOST = ractest2-n1)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 
ractest2-n2)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = ractest2-n3)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = ractest2-n4)(PORT = 1521)))(CONNECT_DATA = (SERVER = 
DEDICATED)(SERVICE_NAME = srv3)(FAILOVER_MODE = (TYPE = SELECT)(METHOD = BASIC)
(RETRIES = 180)(DELAY = 5))))"
username="test3" password="test3" maxActive="100" maxIdle="5"
maxWait="-1"/>

对于这个配置我其实很不理解,然后找dba跟我说了下,说这是oracle做的RAC的,所以画了个小图如下。

IRAC_20161221_154717.jpg

其实对于RAC就是做数据库集群,其中一个连接断掉了,可以切换到其他连接上去。

浩语

                                          __                                                        
                            __  _  ____ __|  |__ _____    ___
                            \ \/ \/ /  |  \  |  \\__  \  /  _ \   
                             \     /|  |  /   Y  \/ __ \(  <_> )
                              \/\_/ |____/|___|  (____  /\____/ 
                                                    \/     \/          
                         2016,To Work Hard,To Adapter,To Change Myself!!!
上一篇下一篇

猜你喜欢

热点阅读