JNDI+Tomcat配置数据源的两种方式

来源:互联网 发布:爱他美淘宝网 编辑:程序博客网 时间:2024/05/29 03:17

非全局jndi配置步骤 :此种配置方式不需要在server.xml中配置数据源,而只在tomcat/conf/Catalina/localhost下的启动配置中配置即可。注意红色字体名称必须和相同。

0、需要在tomcat/common/lib下加入数据库连接的jar包

1、web.xml配置

?
<resource-ref>
     <description>my DB Connection</description>
     <res-ref-name>mydataSource </res-ref-name>  
     <res-type>javax.sql.DataSource</res-type>
     <res-auth>Container</res-auth>
</resource-ref>

2、applicationContext.xml下配置

?
<bean id="dataSource"
    class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName"value="java:comp/env/mydataSource "/>
</bean>

3、在tomcat的conf下的localhost下的配置如下

?
<?xml version="1.0"encoding="UTF-8"?>
<Context docBase="F:/workspace/cuapp/WebRoot"path="cuapp" reloadable="false">
<Resource name="mydataSource"auth="Container"type="javax.sql.DataSource"
                  url="jdbc:oracle:thin:@192.168.2.104:1521:ora10g"
                driverClassName="oracle.jdbc.driver.OracleDriver"
                password="aa"
                username="aa"
                initialSize="2"
                maxActive="3"
                maxIdle="1"
               minIdle="1"
               maxWait="10000"
               removeAbandoned="true"
               logAbandoned="true"
               removeAbandonedTimeout="60"
               timeBetweenEvictionRunsMillis="900000"
               minEvictableIdleTimeMillis="1800000"
               numTestsPerEvictionRun="100"
               validationQuery="select count(0) from dual"
               poolPreparedStatements="true"
               maxOpenPreparedStatements="100"/>
</Context>


全局jndi配置 :此种配置需要在server.xml中配置数据源。

0、需要在tomcat下加入数据库连接的jar包

1、web.xml配置

?
<resource-ref>
    <description>my DB Connection</description>
    <res-ref-name>mydataSource </res-ref-name>   must be same as server.xml
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

2、applicationContext.xml下配置

?
<bean id="dataSource"
    class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName"value="java:comp/env/mydataSource "/>
</bean>

3、server.xml中配置为

?
<!-- Global JNDI resources -->
<GlobalNamingResources>
   <!-- Test entryfor demonstration purposes -->
   <Environment name="simpleValue"type="java.lang.Integer"value="30"/>
   <!-- Editable user database that can also be used by
        UserDatabaseRealm to authenticate users -->
   <Resource name="UserDatabase"auth="Container"
             type="org.apache.catalina.UserDatabase"
      description="User database that can be updated and saved"
          factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
         pathname="conf/tomcat-users.xml"/>
   <Resource name="mydataSource"auth="Container"type="javax.sql.DataSource"
                              url="jdbc:oracle:thin:@192.168.2.104:1521:ora10g"
                          driverClassName="oracle.jdbc.driver.OracleDriver"
                          password="aa"
                          username="aa"
                          initialSize="5"
                          maxActive="10"
                          maxIdle="5"
                          minIdle="2"
                          maxWait="10000"
                          removeAbandoned="true"
                          logAbandoned="true"
                          removeAbandonedTimeout="60"
                          timeBetweenEvictionRunsMillis="900000"
                          minEvictableIdleTimeMillis="1800000"
                          numTestsPerEvictionRun="100"
                          validationQuery="select count(0) from dual"
                          poolPreparedStatements="true"
                          maxOpenPreparedStatements="100"/>
</GlobalNamingResources>

4、tomcat/conf下localhost下的配置如下

?
<?xml version="1.0"encoding="UTF-8"?>
<Context docBase="F:/workspace/cuapp/WebRoot"path="xj-adminportal"reloadable="false">
    <ResourceLink name="mydataSource "global="mydataSource "type="javax.sql.DataSource"/>
</Context>
当一个人找不到出路的时候,最好的办法就是将当前能做好的事情做到极致,做到无人能及。
0 0
原创粉丝点击