Tomcat 6 部署工程总结,使用JNDI数据源配置

时间:2021-12-20 19:39:40
工程需要用JNDI数据源方式部署到tomcat,参考网上文章后,经过配置测试,摸索出来了。
环境说明:
数据库:Oracle9i
Web服务器:tomcat-6.0.33
tomcat启动方式:server.xml配置context上下文启动
持久层框架:JPA
部署工程名:ipeer-interface(JPA,Spring,CXF)
Tomcat部署工程,使用JNDI数据源配置
修改两个地方:server.xml,spring*-dao.xml
1.修改server.xml,在context节点下增加Resouce节点配置
路径:%CATALINA_HOME%\conf\
代码:
<Context path="/ipeer-interface" docBase="G:\workspace\ipeer-interface\WebRoot" reloadable="false">

<Resource name="ipeerDs" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="ipeer_new" password="passw0rd!" driverClassName="oracle.jdbc.driver.OracleDriver"
               url="jdbc:oracle:thin:@1.1.1.1:1555:dbname"/>

</Context>

2.修改spring*-dao.xml,数据源bean配置jndi名字
代码:
       <bean name="localhost_oracleDS,springSecurityDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton" >
             <!-- jboss format -->
<!--        <property name="jndiName" value="java:/ipeerDs"/>-->
             <!-- tomcat format -->
             <property name="jndiName" value="java:comp/env/ipeerDs"/>
       </bean>
这里的jndiName属性配置要重点讲一下,现在JNDI的Resource名字是ipeerDs。
tomcat需要JNDI前缀,java:comp/env,这里配置的话就是:java:comp/env/ipeerDs。
jboss强悍些,两种配置都能行:java:/ipeerDs或java:comp/env/ipeerDs。webLogic与jboss一样。
参考笔记:Web服务器配置注意要点
PS:
1.网上很多文章说需要在web.xml文件中配置一下代码,但是我加与不加,在tomcat6下没有任何影响,一样能成功启动。
       <!-- JNDI数据源,tomcat6可以不需要 -->
       <resource-ref>
             <res-ref-name> ipeerDs</res-ref-name >
             <res-type> javax.sql.DataSource</res-type >
             <res-auth> Container</res-auth >
       </resource-ref>
2.由于这里使用了JPA,所以多了一个persistence.xml文件。工程使用了JTA,jboss的话需要在这个文件中加一个JNDI名字声明配置,而tomcat6加与不加都不会影响到启动。
       <persistence-unit name="localhost_oracle" transaction-type= "JTA">
             <description> generated-persistence-unit</description >
             <provider> org.hibernate.ejb.HibernatePersistence</provider >
             <!-- jboss need -->
             <jta-data-source> java:/ipeerDs</jta-data-source > 
             <class> com.iproject.core.domain.Authorities</class >
             <class> com.iproject.core.domain.Users</class >
             <exclude-unlisted-classes> true</exclude-unlisted-classes >
       </persistence-unit>
3.不管是oracle还是mysql等其他数据库,web服务器部署时是需要它们的驱动jar包的。网上很多说启动一定要在%CATALINA_HOME%\lib下添加驱动jar包。我测试后发现:tomcat部署情况下,只要在工程lib目录或tomcat lib目录任一地方添加了驱动jar包,启动就没问题。不一定tomcat lib目录下一定要有数据库驱动jar包。
JNDI配置相关文件:
persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

<persistence-unit name="localhost_oracle" transaction-type="JTA">
<description>generated-persistence-unit</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<!-- jboss need -->
<!-- <jta-data-source>java:/ipeerDs</jta-data-source> -->

<class>com.iproject.core.domain.Authorities</class>
<class>com.iproject.core.domain.Users</class>
<class>com.iproject.core.domain.TSysFunction</class>
<class>com.iproject.core.domain.TSysLog</class>
<class>com.iproject.core.domain.TSysSeriano</class>
<class>com.iproject.core.domain.TBicAgtPrmInfo</class>
<class>com.iproject.core.domain.TBicFinChargebill</class>
<class>com.iproject.core.domain.TSysDept</class>
<class>com.iproject.core.domain.Domain</class>
<class>com.iproject.core.domain.DomainValue</class>
<class>com.iproject.core.domain.TBicPrdTyp</class>
<class>com.iproject.core.domain.TBicCancelInfo</class>
<class>com.iproject.core.domain.SynchronizationTask</class>
<class>com.iproject.core.domain.ProcessDefinition</class>
<class>com.iproject.core.domain.ProcessInstance</class>
<class>com.iproject.core.domain.TBicPrmPlyRel</class>
<class>com.iproject.core.domain.TBicPrmPlyRelId</class>
<class>com.iproject.core.domain.TBicResConfig</class>
<class>com.iproject.core.domain.TBicLog</class>
<class>com.iproject.core.domain.TBicEmp</class>
<class>com.iproject.core.domain.TBasTrdCode</class>
<class>com.iproject.core.domain.PlyuploadLob</class>
<class>com.iproject.core.domain.TBicPlyMain</class>
<class>com.iproject.core.domain.TBicPrdTypCvrgRel</class>
<class>com.iproject.core.domain.TBicPrdTypRel</class>
<class>com.iproject.core.domain.TBicPrmCompute</class>
<class>com.iproject.core.domain.TBicPrmComputeSet</class>
<class>com.iproject.core.domain.TBicEmpCombo</class>
<class>com.iproject.core.domain.TBicVariflightComm</class>
<class>com.iproject.core.domain.TSrcProdSpecRel</class>
<class>com.iproject.core.domain.TBicRateSetSpl</class>
<class>com.iproject.core.domain.TBicElecAir</class>
<class>com.iproject.core.domain.TBicPacketLog</class>

<exclude-unlisted-classes>true</exclude-unlisted-classes>

</persistence-unit>
</persistence>

server.xml

<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">

<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
<Listener className="org.apache.catalina.core.JasperListener" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- 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" />
</GlobalNamingResources>

<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">

<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->

<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation -->
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->

<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->

<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">

<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->

<!-- The request dumper valve dumps useful debugging information about
the request and response data received and sent by Tomcat.
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.valves.RequestDumperValve"/>
-->

<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>

<!-- Define the default virtual host
Note: XML Schema validation will not work with Xerces 2.2.
-->
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">

<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->

<!-- Access log processes all example.
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
-->

<!--
<Context path="/peerv6_test" docBase="G:\workspace\peerv6_test\WebRoot" reloadable="false"/>
-->

<Context path="/ipeer-interface" docBase="G:\workspace\ipeer-interface\WebRoot" reloadable="false">

<Resource name="ipeerDs" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="ipeer_new" password="passw0rd!" driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@1.1.1.1:1555:dbname"/>

</Context>

<!--
<Context path="/ipeer-task" docBase="G:\workspace\ipeer-task\WebRoot" reloadable="false"/>
-->

<!--
<Context path="/ipeer" docBase="G:\workspace\ipeer\WebRoot" reloadable="false"/>
-->

</Host>

</Engine>
</Service>
</Server>

spring-dao-context.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">

<!-- ******************************************************************** -->
<!-- Scan for service layer annotated beans -->
<!-- ******************************************************************** -->
<context:component-scan base-package="com.iproject.core.dao" scoped-proxy="interfaces" />

<!-- ******************************************************************** -->
<!-- PropertyConfigurer for the DAO -->
<!-- ******************************************************************** -->
<context:property-placeholder location="classpath:standard-dao.properties" />

<!-- ******************************************************************** -->
<!-- Setup the transaction manager -->
<!-- ******************************************************************** -->
<!-- Using Atomikos Transaction Manager -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager" init-method="init" destroy-method="close">
<property name="forceShutdown" value="true" />
<property name="startupTransactionService" value="true" />
<property name="transactionTimeout" value="60" />
</bean>

<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp" />

<!-- Configure the Spring framework to use JTA transactions from Atomikos -->
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager" />
<property name="userTransaction" ref="atomikosUserTransaction" />
<!-- <property name="transactionSynchronizationName" value="SYNCHRONIZATION_ON_ACTUAL_TRANSACTION" /> -->
</bean>

<!-- 启动注解功能 -->
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- ******************************************************************** -->
<!-- Setup a data source -->
<!-- ******************************************************************** -->
<bean name="localhost_oracleDS,springSecurityDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">
<!-- jboss format -->
<!-- <property name="jndiName" value="java:/ipeerDs"/>-->
<!-- tomcat format -->
<property name="jndiName" value="java:comp/env/ipeerDs"/>
</bean>

<!-- Using Apache DBCP Data Sources -->
<!-- <bean class="org.apache.commons.dbcp.BasicDataSource"-->
<!-- destroy-method="close" name="localhost_oracleDS,springSecurityDataSource">-->
<!-- <property name="driverClassName" value="${localhost_oracle.connection.driver_class}" />-->
<!-- <property name="username" value="${localhost_oracle.connection.username}" />-->
<!-- <property name="password" value="${localhost_oracle.connection.password}" />-->
<!-- <property name="url" value="${localhost_oracle.connection.url}" />-->
<!-- <property name="maxIdle" value="${localhost_oracle.minPoolSize}" />-->
<!-- <property name="maxActive" value="${localhost_oracle.maxPoolSize}" />-->
<!-- </bean>-->

<!-- ******************************************************************** -->
<!-- Setup each persistence unit -->
<!-- ******************************************************************** -->
<!-- Configure a JPA vendor adapter -->
<bean id="localhost_oracleJPAVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="${localhost_oracle.show_sql}" />
<property name="generateDdl" value="${localhost_oracle.generateDdl}" />
<property name="databasePlatform" value="${localhost_oracle.dialect}" />
</bean>
<!-- EntityManager Factory that brings together the persistence unit, datasource,
and JPA Vendor -->
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="localhost_oracle">
<property name="dataSource" ref="localhost_oracleDS" />
<property name="persistenceUnitName" value="localhost_oracle" />
<property name="jpaVendorAdapter" ref="localhost_oracleJPAVendorAdapter" />
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.transaction.manager_lookup_class"
value="com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup" />
<entry key="hibernate.connection.release_mode" value="on_close" />
</map>
</property>
</bean>

<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="localhost_oracle" />
</bean>

<bean id="commonDAO" class="com.iproject.core.dao.CommonDAO">
<property name="entityManagerFactory" ref="localhost_oracle" />
</bean>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<display-name>ipeer-interface</display-name>
<description>永诚银保通接口平台</description>

<!-- JNDI数据源,tomcat6可以不需要 -->
<!-- <resource-ref>-->
<!-- <res-ref-name>ipeerDs</res-ref-name>-->
<!-- <res-type>javax.sql.DataSource</res-type>-->
<!-- <res-auth>Container</res-auth>-->
<!-- </resource-ref>-->

<context-param>
<description></description>
<param-name>EAPConfig</param-name>
<param-value>/WEB-INF/EAPConfig.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring刷新Interceptor防止内存泄漏 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<!-- 启动Socket服务 -->
<listener>
<listener-class>com.iproject.core.restful.SocketServiceInit</listener-class>
</listener>

<filter>
<description>generated-persistence-filter</description>
<filter-name>localhost_oracleFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
<init-param>
<param-name>entityManagerFactoryBeanName</param-name>
<param-value>localhost_oracle</param-value>
</init-param>
</filter>

<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>localhost_oracleFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>InitParameterServlet</servlet-name>
<servlet-class>com.iproject.core.fwk.web.servlet.InitParameterServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>

<servlet>
<servlet-name>EAPConfigServlet</servlet-name>
<servlet-class>com.iproject.core.fwk.web.servlet.EAPConfigServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- webservice -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>/pages/login.jsp</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/pages/error.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/pages/error.jsp</location>
</error-page>

</web-app>