数据源配置心得体会

原创 2005年05月24日 20:49:00

其实在tomcat5下配置数据源的帖子网上也有很多,但是基本上一次成功的不多,为什么呢? 主要可能有以下几个原因:

1。xml文件配置不正确

2。通过图形化界面去配置(事实证明,通过图形化界面去配置数据源常常会引起bug,所以还是推荐用手动去配置)

3。程序迁移时,第三方的jar的位置放置是否正确(大多数情况下,开发人员可能是把一个现成的应用移植到tomcat服务器上来而遇到了种种原因。)

配置基于DBCP的数据源,主要需要配置server.xml,另外,在web.xml里,也要做resource-ref的配置,也可以不配,但是按照规范,推荐配置。

一个已经配置成功的server.xml如下:

<?xml version='1.0' encoding='utf-8'?>
<Server>
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
  <GlobalNamingResources>
    <Environment name="simpleValue" type="java.lang.Integer" value="30"/>
    <Resource auth="Container" description="User database that can be updated and saved" name="UserDatabase" type="org.apache.catalina.UserDatabase"/>
    <ResourceParams name="UserDatabase">
      <parameter>
        <name>factory</name>
        <value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
      </parameter>
      <parameter>
        <name>pathname</name>
        <value>conf/tomcat-users.xml</value>
      </parameter>
    </ResourceParams>
  </GlobalNamingResources>
  <Service name="Catalina">
    <Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" port="8081" redirectPort="8443" maxSpareThreads="75" maxThreads="150" minSpareThreads="25">
    </Connector>
    <Connector port="8009" protocol="AJP/1.3" protocolHandlerClassName="org.apache.jk.server.JkCoyoteHandler" redirectPort="8443">
    </Connector>
    <Engine defaultHost="localhost" name="Catalina">
      <Host appBase="webapps" name="localhost">
        <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_log." suffix=".txt" timestamp="true"/>
<Context path="/PUB_J2EE" docBase="PUB_J2EE" debug="5" reloadable="true" crossContext="true">

 <Logger className="org.apache.catalina.logger.FileLogger" prefix="localhost_PUBJ2EE_log." suffix=".txt" timestamp="true"/>
          <Resource name="jdbc/sqldb" auth="Container" type="javax.sql.DataSource"/>
    <Resource name="jdbc/file" auth="Container" type="javax.sql.DataSource"/>
          <ResourceParams name="jdbc/sqldb">
                   <parameter>
                               <name>factory</name>
                               <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
                   </parameter>

                                <!-- Class name for mm.mysql JDBC driver -->
                   <parameter>
                                <name>driverClassName</name>
                                <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</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:microsoft:sqlserver://localhost:1433;DatabaseName=PUB_J2EE</value>
                   </parameter>

                                <!-- MySQL dB username and password for dB connections  -->
                   <parameter>
                                <name>username</name>
                                <value>sa</value>
                   </parameter>

                   <parameter>
                                <name>password</name>
                                <value>sa</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>
        </ResourceParams>
  <ResourceParams name="jdbc/file">
                   <parameter>
                               <name>factory</name>
                               <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
                   </parameter>

                                <!-- Class name for mm.mysql JDBC driver -->
                   <parameter>
                                <name>driverClassName</name>
                                <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</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:microsoft:sqlserver://localhost:1433;DatabaseName=FileSource</value>
                   </parameter>

                                <!-- MySQL dB username and password for dB connections  -->
                   <parameter>
                                <name>username</name>
                                <value>sa</value>
                   </parameter>

                   <parameter>
                                <name>password</name>
                                <value>sa</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>
        </ResourceParams>
  </Context>
      </Host>
      <Logger className="org.apache.catalina.logger.FileLogger" prefix="catalina_log." suffix=".txt" timestamp="true"/>
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"/>
    </Engine>
  </Service>
</Server>

Mongo的Replica Sets (复制集)的配置全过程和心得体会

一、MongoDB Replica Sets(副本集)简单的说就是有自动故障恢复功能的主从集群,主从集群和副本集的最为明显的区别是副本集没有固定的"主节点":整个集群会选举出来一个"主节点"当其不能正...

vs2010配置opencv之心得体会

前几天刚接触opencv,自己想看看opencv到底功能有多强大!于是乎下载了一个最新版本的(opencv2.4.2,当时是最新的,于2012.10.20)。加上visual studio 2010 ...

C语言学习历程(一)数据类型及变量之心得体会

数据类型 以往我学C++的时候,只知道有三种基本的数据类型:字符型(char),整型(int),浮点型(float、double)。却并不知道数据类型是从何而来,并且其作用是什么。然而,这一次经...

静态数据static的初始化的要点及个人心得体会

这是Java中一个非常重要的概念,只要理清其中的优先级顺序就能很好的掌握,具体内容可由以下的这段代码加以理解: class Bowl{ Bowl(int market){ System.out...

关于机载LiDAR数据处理的几点心得体会

首先声明一下,之所以开通这个博客,然后写点东西,真是有点被逼无奈的味道。想下载别人的资源,可是手头没分,不想再麻烦别人帮忙下载,索性自己动手,丰衣足食吧。            在硕士期间,一直做机...

基于注解的Spring多数据源配置和使用

前一段时间研究了一下spring多数据源的配置和使用,为了后期从多个数据源拉取数据定时进行数据分析和报表统计做准备。由于之前做过的项目都是单数据源的,没有遇到这种场景,所以也一直没有去了解过如何配置多...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:数据源配置心得体会
举报原因:
原因补充:

(最多只允许输入30个字)