关闭

如何把hibernate+jndi+tomcat发布自己的web应用

1184人阅读 评论(0) 收藏 举报
一.在tomcat_home/common/lib下放入jdbc的驱动程序,额外说一下,如果是使用sql server的话,有至少两个驱动可以选择,一个是微软提供的,另一个是 jtds,比微软的要好很多,推荐使用
二.配置文件,tomcat 不同的版本配置文件略有不同,下面以tomcat5..5.*为例。
三.如果配置不正确会出现javax.naming.NameNotFoundException: Name is not bound in this Context 错误
方式一、全局数据库连接池
方式一:
1、通过管理界面配置连接池,或者直接在tomcat/conf/server.xml的GlobalNamingResources中增加
MSSQL:
<Resource name="jdbc/mydb" type="javax.sql.DataSource" password="mypwd" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" maxIdle="2" maxWait="5000" validationQuery="select 1" username="sa" url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb" maxActive="4"/>
Oracle:
<Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/edu" type="javax.sql.DataSource" url="jdbc:oracle:thin:@10.8.6.145:1521:orcl" username="edusys" password="edusys" />
注意:注释掉<context> 中的<Resource>
2、在tomcat/webapps/myapp/META-INF/context.xml的Context中增加:
<ResourceLink global="jdbc/mydb" name="jdbc/mydb" type="javax.sql.DataSource"/>
方式二:全局数据库连接池
1、同上
2、在tomcat/conf/context.xml的Context中增加:
<ResourceLink global="jdbc/mydb" name="jdbc/mydb" type="javax.sql.DataSource"/>
方式三:局部数据库连接池
只需在tomcat/webapps/myapps/META-INF/context.xml的Context中增加:
<Resource name="jdbc/mydb" type="javax.sql.DataSource" password="mypwd" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" maxIdle="2" maxWait="5000" validationQuery="select 1" username="sa" url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb" maxActive="4"/>
参数说明:
driveClassName:JDBC驱动类的完整的名称;
maxActive:同时能够从连接池中被分配的可用实例的最大数;
maxIdle:可以同时闲置在连接池中的连接的最大数;
maxWait:最大超时时间,以毫秒计;
password:用户密码;
url:到JDBC的URL连接;
user:用户名称;
validationQuery:用来查询池中空闲的连接。
以上三种方式在tomcat 5.5.4下都可以。另外,sql server的jdbc driver是从微软网站上下载的sql server jdbc (sp3)。

下面 分别举tomcat5.0 和tomcat 5.5  配置文件的例子(将此配置文件置于Tomcat /conf/Catalina/localhost 下)
tomcat5.0
 <Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"/>
    <ResourceParams name="jdbc/test">
        <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>sa</value>
        </parameter>
        <parameter>
            <name>password</name>
            <value>test</value>
        </parameter>
        <!-- Class name for JDBC driver -->
        <parameter>
            <name>driverClassName</name>
            <value>net.sourceforge.jtds.jdbc.Driver</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:jtds:sqlserver://url/filedb;charset=gb2312;autoReconnect=true</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>
//***********************************************************************************************
tomcat 5.5
  <Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"
              maxActive="100" maxIdle="30" maxWait="10000"
              driverClassName="oracle.jdbc.driver.OracleDriver"
              username="test" password="test"
              url="jdbc:oracle:thin:@url:1521:dcdb"
              defaultAutoCommit="true" removeAbandoned="true"
              removeAbandonedTimeout="60" logAbandoned="true"/>
可以看到 5.0 中原来是元素的项在5.5中变成了属性
另外:在自己的web应用中的web.xml中加入 (web-app元素下)
 <resource-ref>
        <description>Oracle Datasource example</description>
        <res-ref-name>jdbc/edu</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Unshareable</res-sharing-scope>
    </resource-ref>
如果是采用的hibernate编写的程序.还要将下面两个文件放在tomcat的bin下面.
hibernate-mapping-3.0.dtd
hibernate-configuration-3.0.dtd
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:197705次
    • 积分:2320
    • 等级:
    • 排名:第16068名
    • 原创:16篇
    • 转载:128篇
    • 译文:0篇
    • 评论:40条
    最新评论