Tomcat应用服务器的开发环境下,数据源的配置往往都是在applicationContext.xml中配置一个dataSource的bean,然后在部署时再修改JNDI数据源配置信息。
最近因为全文检索DIH增量索引数据源配置问题,总结一下。
Tomcat6的服务器配置文件放在 ${tomcat6}/conf 目录底下。这里主要涉及两个配置文件 server.xml 和 context.xml。
在tomcat6版本中,context元素从server.xml文件中独立出来,放在一个context.xml文件中。这是因为server.xml是不可动态重加载的资源,服务器一旦启动了以后,要修改这个文件,就得重启服务器才能重新加载。而context.xml文件则不然,tomcat服务器会定时去扫描这个文件。一旦发现文件被修改(时间戳改变了),就会自动重新加载这个文件,而不需要重启服务器。 所以我们推荐把应用需要的JNDI资源配置在context.xml文件中,而不是server.xml中。
全文检索数据源配置
1.TOMCAT context.xml 中添加数据源
<pre name="code" class="html"><?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.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context allowLinking="true">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Resource name ="jdbc/core1" auth = "Container"type = "javax.sql.DataSource"
driverClassName = "com.mysql.jdbc.Driver"
url="jdbc:mysql://ip:port/db_name?characterEncoding=UTF-8" username="root" password="root"/>
<Resource name ="jdbc/core2" auth = "Container" type = "javax.sql.DataSource"
driverClassName = "com.mysql.jdbc.Driver"
url="jdbc:mysql://ip:port/db_name?characterEncoding=UTF-8" username="root" password="root" />
</Context>
2.Solr data-config.xml中添加对数据源的引用
源代码:
<dataSource name="jdbc" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://ip:port/db_name?characterEncoding=UTF-8" user="root" password="root"/>
修改后:
<dataSource name="jdbc" jndiName="java:comp/env/jdbc/core1" type="JdbcDataSource" batch-size="1000" />
TOMCAT数据源配置总结
以下转载自 :http://blog.csdn.net/dyllove98/article/details/7706218
第一种,单个应用独享数据源
就一步,找到Tomcat的server.xml找到工程的Context节点,添加一个私有数据源
<Context docBase="WebApp" path="/WebApp" reloadable="true" source="org.eclipse.jst.jee.server:WebApp">
<Resource
name="jdbc/mysql"
scope="Shareable"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
url="jdbc:mysql://localhost:3306/test"
driverClassName ="com.mysql.jdbc.Driver"
username="root"
password="root"
/>
</Context>
优点:简单 缺点:重用性差
第二种,配置全局JNDI数据源,应用到单个应用
第一步, 找到Tomcat的server.xml中GlobalNamingResources节点,在节点下加一个全局数据源<Resource
name="jdbc/mysql"
scope="Shareable"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
url="jdbc:mysql://localhost:3306/test"
driverClassName ="com.mysql.jdbc.Driver"
username="root"
password="root"
/>
第二步,找到要应用此JNDI数据源的工程Context节点,增加对全局数据源的引用ResourceLink
<Context docBase="WebApp" path="/WebApp" reloadable="true">
<ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.DataSource" />
</Context>
第三种,配置全局JNDI数据源,应用到所有Tomcat下部署的应用
也分两步第一步 参考第二种的第一步
第二步,找到Tomcat的context.xml,在Context节点下加一个ResourceLink节点对第一步配置的数据源进行引用
这个XML配置文件的根节点就是<Context>
<Context>
<ResourceLink global="jdbc/mysql" name="jdbc/mysql" type="javax.sql.DataSource" />
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Context>
优点:重用性,一次性到位 缺点:没有可控性
Spring对JNDI数据源的引用
在applicationContext.xml中加一个bean,替代原来的dataSource
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/mysql" />