JNDI+Tomcat配置數據源的兩種方式


 

非全局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 entry for 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>
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM