转载 2012年03月21日 17:03:30

            1.1 Context configuration:
<Context path="/DBTest" docBase="DBTest"
        debug="5" reloadable="true" crossContext="true"<
        <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
             1.2 web.xml configuration:
      <description<DB Connection</description<
      <res-auth<Container</res-auth<     / /据说还可以是Application
            1.3 Test code 还可以在jsp中直接使用数据源
<%@ taglib uri="" prefix="sql" %<
<%@ taglib uri="" prefix="c" %<
<sql:query var="rs" dataSource="jdbc/TestDB"<
        select id, foo, bar from testdata
<c:forEach var="row" items="${rs.rows}"<
    Foo ${}<br/<
    Bar ${}<br/<

        2. Oracle
            2.1 Context configuration:
<Resource name="jdbc/myoracle" auth="Container"
              type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              username="scott" password="tiger" maxActive="20" maxIdle="10"
            2.2 web.xml configuration:
    <description<Oracle Datasource example</description<
            2.3 Test code
Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();

        把配置放在Context 标签对里面只对当前应用有效,要想对整个TomCat有效,可以放到GlobalNamingResources里面。(强烈申明,此种情况好像是5.0版本前是这样设置的,5.5和6.0版本如果要是全局的话,就在Tomcat_Home\conf下的context.xml文件直接配置)

        Tomcat6的连接池设置有一些不一样,在server.xml中文配置老是报Cannot create JDBC driver of class '' for connect URL 'null' 的错误,需要将Tomcat6安装路径的conf目录下的context.xml文件拷贝到自己应用的META-INF目录下,并添加<Resource /<的配置。

Intermittent dB Connection Failures:
Tomcat runs within a JVM. The JVM periodically performs garbage collection (GC) to remove java objects which are no longer being used. When the JVM performs GC execution of code within Tomcat freezes. If the maximum time configured for establishment of a dB connection is less than the amount of time garbage collection took you can get a db conneciton failure. 
        To collect data on how long garbage collection is taking add the -verbose:gc argument to your CATALINA_OPTS environment variable when starting Tomcat. When verbose gc is enabled your $CATALINA_BASE/logs/catalina.out log file will include data for every garbage collection including how long it took.
        When your JVM is tuned correctly 99% of the time a GC will take less than one second. The remainder will only take a few seconds. Rarely, if ever should a GC take more than 10 seconds.
        Make sure that the db connection timeout is set to 10-15 seconds. For the DBCP you set this using the parameter maxWait.
Random Connection Closed Exceptions:
        These can occur when one request gets a db connection from the connection pool and closes it twice. When using a connection pool, closing the connection just returns it to the pool for reuse by another request, it doesn't close the connection. And Tomcat uses multiple threads to handle concurrent requests. Here is an example of the sequence of events which could cause this error in Tomcat:
  Request 1 running in Thread 1 gets a db connection.
  Request 1 closes the db connection.
  The JVM switches the running thread to Thread 2
  Request 2 running in Thread 2 gets a db connection
  (the same db connection just closed by Request 1).
  The JVM switches the running thread back to Thread 1
  Request 1 closes the db connection a second time in a finally block.
  The JVM switches the running thread back to Thread 2
  Request 2 Thread 2 tries to use the db connection but fails
  because Request 1 closed it.
Here is an example of properly written code to use a db connection obtained from a connection pool:
Connection conn = null;
  Statement stmt = null;  // Or PreparedStatement if needed
  ResultSet rs = null;
  try {
    conn = ... get connection from connection pool ...
    stmt = conn.createStatement("select ...");
    rs = stmt.executeQuery();
    ... iterate through the result set ...
    rs = null;
    stmt = null;
    conn.close(); // Return to connection pool
    conn = null;  // Make sure we don't close it twice
  } catch (SQLException e) {
    ... deal with errors ...
  } finally {
    // Always make sure result sets and statements are closed,
    // and the connection is returned to the pool
    if (rs != null) {
      try { rs.close(); } catch (SQLException e) { ; }
      rs = null;    //方便gc进行垃圾回收
    if (stmt != null) {
      try { stmt.close(); } catch (SQLException e) { ; }
      stmt = null;
    if (conn != null) {
      try { conn.close(); } catch (SQLException e) { ; }
      conn = null;


  1. <!-- 通过JNDI 使用应用服务器 的Connection Pool--<  
  2. <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"<  
  3.     <property name="jndiName" value="java:comp/env/jdbc/coral"/<  
  4. </bean< 



  • 2017年03月23日 15:55
  • 1.71MB
  • 下载


  • 2017年03月10日 17:25
  • 1.7MB
  • 下载




  • 2015年11月15日 11:17
  • 63KB
  • 下载


1、事务就是mysql里说的那个事务。——比如我们在一组操作中故意设置个异常,导致第一个执行了第二个没执行。 public void doGet(HttpServletRequest reque...


  • 2015年01月05日 21:01
  • 77KB
  • 下载


  • 2014年06月13日 10:22
  • 2KB
  • 下载


首先先对tomcat中的一些配置文件进行修改 1、创建数据库shixun 2、配置server.xml文件。Tomcat安装目录下conf中server.xml文件。 ...


  • 2014年06月06日 20:08
  • 1.11MB
  • 下载