Tomcat中JDBCRealm的配置

来源:互联网 发布:javascript权威指南 7 编辑:程序博客网 时间:2024/05/17 05:12
在配置Tomcat的JDBCRealm的时候,有几个应该注意的地方

   1. 数据库JDBC驱动应该放在${tomcat.home}/server/lib目录下,而不能放在webapps的lib目录下,二者的class loader不同
   2. tomcat在启动时只会自动装载后缀为.jar的jar包,因此如果使用oracle9i的驱动,应把class12.zip改名为class12.jar
   3. 最好将JDBCRealm的配置信息放在webapp的context配置文件中,而不要放在server.xml中


e.g.
<!-- ======================================================================= -->
<!-- This file is deployed to Tomcat 4.1.x using the "setup-tomcat" ant      -->
<!-- task. You can also manually copy it to Tomcat's webapps directory.      -->
<!-- ======================================================================= -->

<!-- To use Tomcat's MemoryRealm (in $CATALINA_HOME/conf/tomcat-users.xml)
     simply comment out both of the Realm's below.  MemoryRealm is the
     default realm for all applications, specified in
     $CATALINA_HOME/conf/server.xml.
    
     To use Tomcat's JDBCRealm, JNDIRealm or JAASRealm, uncomment the
     appropriate section below.  All of these 3 realms require supplemental
     drivers (JAR files) in $CATALINA_HOME/common/lib.
    
     jdbc/mysql: http://www.mysql.com/downloads/api-jdbc-stable.html
                 - JAR: mysql-connector-java-2.0.14-bin.jar
     jdbc/postgresql: http://www.postgresql.org/
                 - JAR: postgresql-jdbc3-7.3.jar
     jndi/ldap:  http://java.sun.com/products/jndi/index.html#download
                 - JAR: ldap.jar
     jaas:       http://free.tagish.net/jaas/index.jsp
                 - JAR: tagishauth.jar

     NOTE: For the JAASRealm, you must also add the path to NTSystem.dll to
     your $PATH environment variable.
    
-->

<!-- Simchronize-selfcare Application Context -->
<Context path="/simchronize-selfcare" docBase="simchronize-selfcare" debug="99" reloadable="true">
    <Logger className="org.apache.catalina.logger.FileLogger"
        prefix="simchronize_selfcare_log." suffix=".txt" timestamp="true"/>

    <!--
       
    <Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
          driverName="oracle.jdbc.driver.OracleDriver"
       connectionURL="jdbc:oracle:thin:@10.168.195.62:1521:SIMCHRO"
      connectionName="SIMCHR_24" connectionPassword="SIMCHR_24"
           userTable="SIMCHR_SELFCARE_USERS" userNameCol="MSISDN" userCredCol="PASSWD"
       userRoleTable="SIMCHR_SELFCARE_USER_ROLES" roleNameCol="ROLE" />
   -->

    <!-- NOTE: If you want to use a DataSourceRealm, the Resource must be a
               global resource -->
    <Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
          driverName="oracle.jdbc.driver.OracleDriver"
       connectionURL="jdbc:oracle:thin:@10.168.195.62:1521:SIMCHRO"
      connectionName="SIMCHR_24" connectionPassword="SIMCHR_24"
           userTable="SIMCHR_SELFCARE_USERS" userNameCol="MSISDN" userCredCol="PASSWD"
       userRoleTable="SIMCHR_SELFCARE_USER_ROLES" roleNameCol="ROLE" />
      
    <Resource name="jdbc/simchronize" auth="Container" type="javax.sql.DataSource"/>
    <ResourceParams name="jdbc/simchronize">
        <parameter>
            <name>factory</name>
            <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
        </parameter>
        <!-- Maximum number of dB connections in pool. Make sure you
             configure your mysqld max_connections large enough to handle
             all of your db connections. Set to 0 for no limit.
             -->
        <parameter>
            <name>maxActive</name>
            <value>100</value>
        </parameter>
        <!-- Maximum number of idle dB connections to retain in pool.
             Set to 0 for no limit.
             -->
        <parameter>
            <name>maxIdle</name>
            <value>30</value>
        </parameter>
        <!-- Maximum time to wait for a dB connection to become available
             in ms, in this example 10 seconds. An Exception is thrown if
             this timeout is exceeded.  Set to -1 to wait indefinitely.
             -->
        <parameter>
            <name>maxWait</name>
            <value>10000</value>
        </parameter>
        <!-- MySQL dB username and password for dB connections  -->
        <parameter>
            <name>username</name>
            <value>SIMCHR_24</value>
        </parameter>
        <parameter>
            <name>password</name>
            <value>SIMCHR_24</value>
        </parameter>
        <!-- Class name for JDBC driver -->
        <parameter>
            <name>driverClassName</name>
            <value>oracle.jdbc.driver.OracleDriver</value>
        </parameter>
        <!-- Autocommit setting.  This setting is required to make
             Hibernate work.  Or you can remove calls to commit(). -->
        <parameter>
            <name>defaultAutoCommit</name>
            <value>true</value>
        </parameter>
        <!-- The JDBC connection url for connecting to your MySQL dB.
             The autoReconnect=true argument to the url makes sure that the
             mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
             connection.  mysqld by default closes idle connections after 8 hours.
             -->
        <parameter>
            <name>url</name>
            <value>jdbc:oracle:thin:@10.168.195.62:1521:SIMCHRO</value>
        </parameter>
        <!-- Recover abandoned connections -->
        <parameter>
            <name>removeAbandoned</name>
            <value>true</value>
        </parameter>
        <!-- Set the number of seconds a dB connection has been idle
             before it is considered abandoned.
             -->
        <parameter>
            <name>removeAbandonedTimeout</name>
            <value>60</value>
        </parameter>
        <!-- Log a stack trace of the code which abandoned the dB
             connection resources.
             -->
        <parameter>
            <name>logAbandoned</name>
            <value>true</value>
        </parameter>
    </ResourceParams>
 
</Context>