系统架构为Tomcat5.0.28+JDK1.4.2,数据库为Sqlserver2000+sp3,数据库连接池采用的是JTA。服务启动后数据库连接正常,但运行一段时间(2-5天不等)后Tomcat报:“连接数据库错误”。重启Tomcat后,有时会好一段时间,有时几分钟后就又出现连接不上数据库的错误。只有重启系统(Win2000和2003都用过)才能正常运行。
   困扰我好长时间了,请各位大侠指教!!!,不胜感激,高分送上

解决方案 »

  1.   

    听上去有点像网络不稳定。
    难道就只有这些日志:Tomcat报:“连接数据库错误”。
    多些日志可能容易解决些。
      

  2.   

    主要原因是网络不稳定,不过连接池的功能也不到位,我以前也碰到过,后来使用hibernate就没这个问题,我特意断网又连网,hibernate可以自动恢复连接.
      

  3.   

    谢谢两位!
    sql2000和Tomcat是装在同一台服务器上的,出现问题后直接用服务器的IE打开,问题依旧。
    现把日志贴出来一些:这是我自己记录的
    警告: java.sql.SQLException: Cannot get connection for URL jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=DB3 : [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.以下是Tomcat在这个时刻记录的:
    2008-09-06 21:18:18 StandardContext[/servlets-examples]ContextListener: attributeReplaced('org.apache.catalina.WELCOME_FILES', '[Ljava.lang.String;@1b1cf43')
    2008-09-06 21:18:18 StandardContext[/servlets-examples]ContextListener: attributeReplaced('org.apache.catalina.WELCOME_FILES', '[Ljava.lang.String;@9e3ba7')
    2008-09-06 21:18:18 StandardContext[/servlets-examples]ContextListener: attributeReplaced('org.apache.catalina.WELCOME_FILES', '[Ljava.lang.String;@5e66cc')
    2008-09-06 21:18:18 StandardContext[/servlets-examples]SessionListener: contextDestroyed()
    2008-09-06 21:18:18 StandardContext[/servlets-examples]ContextListener: contextDestroyed()
    2008-09-06 21:18:18 StandardContext[/jsp-examples]ContextListener: attributeReplaced('org.apache.catalina.WELCOME_FILES', '[Ljava.lang.String;@c99912')
    2008-09-06 21:18:18 StandardContext[/jsp-examples]ContextListener: attributeReplaced('org.apache.catalina.WELCOME_FILES', '[Ljava.lang.String;@160983a')
    2008-09-06 21:18:18 StandardContext[/jsp-examples]ContextListener: attributeReplaced('org.apache.catalina.WELCOME_FILES', '[Ljava.lang.String;@aa492b')
    2008-09-06 21:18:18 StandardContext[/jsp-examples]SessionListener: contextDestroyed()
    2008-09-06 21:18:18 StandardContext[/jsp-examples]ContextListener: contextDestroyed()
    2008-09-06 21:19:02 StandardContext[/balancer]Exception starting filter BalancerFilter
    java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester
    at org.apache.webapp.balancer.RulesParser.createDigester(RulesParser.java:65)
    at org.apache.webapp.balancer.RulesParser.<init>(RulesParser.java:43)
    at org.apache.webapp.balancer.BalancerFilter.init(BalancerFilter.java:79)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:225)
    at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:308)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:79)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3698)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4349)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:823)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:807)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:595)
    at org.apache.catalina.core.StandardHostDeployer.install(StandardHostDeployer.java:277)
    at org.apache.catalina.core.StandardHost.install(StandardHost.java:832)
    at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:701)
    at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:432)
    at org.apache.catalina.startup.HostConfig.start(HostConfig.java:983)
    at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:349)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1091)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:789)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:478)
    at org.apache.catalina.core.StandardService.start(StandardService.java:480)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:2313)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:556)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:324)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:287)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:425)
    连接池的配置如下:
           <Context path="/" docBase="D:/XXXXX" debug="0"
                     reloadable="false" crossContext="true">
      <Resource name="UserTransaction" auth="Container" type="javax.transaction.UserTransaction" />
      <ResourceParams name="UserTransaction">
        <parameter>
          <name>factory</name>
    <value>org.objectweb.jotm.UserTransactionFactory</value>
        </parameter>
        <parameter>
          <name>jotm.timeout</name>
    <value>30</value>
       </parameter>
    </ResourceParams>                 
           </Context>  
      

  4.   

    java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester 说明缺少commons-digester.jar包
      

  5.   

    我觉得跟网络没关系应该是设置了断开的命令就好象session过期一样
      

  6.   

    Sqlserver2000一定要打sp4,
    另外,要使用最新的驱动
    http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=c47053eb-3b64-4794-950d-81e1ec91c1ba另外注意连接写法与以前的差异
    http://msdn.microsoft.com/zh-cn/library/ms378749.aspx
      

  7.   

    "Error establishing socket" exception while connecting to the database
    Symptom: while running a windows application that includes massive opening and closing of a connections to database server, "Error establishing socket" exception appears after a short time of execution. Cause: This problem is caused by a lack of a reusable sockets on client machine, since each TCP/IP connection stays in the TIME_WAIT state when the connection is being closed. While a connection is in this state, the socket cannot be reused. Resolution: do one of the following: Increase the port range that is used for anonymous ports to approximately 20,000 ports (for example) by modifying the MaxUserPort registry key (this parameter controls the maximum port number that is used when an application requests any available user port from the system). Windows uses the conventional BSD range of 1024 to 5000 for its anonymous (ephemeral) port range. You can set only the upper bound of the ephemeral port range. To modify the MaxUserPort registry key, follow these steps: 
    Start Registry Editor (Regedt32.exe). Please note that you should backup your registry and any important files on your computer before editing the registry. 
    Locate the MaxUserPort key in the registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters 
    On the Edit menu, click Add Value, and then add the following registry value: 
    Value Name: MaxUserPort 
    Data Type: REG_DWORD 
    Value: 65534 (for example) 
    Valid Range: 5000-65534 (decimal) 
    Default: 0x1388 (5000 decimal) 
    Quit Registry Editor. 
    Change the timeout on the connections from 240 seconds (the default) to any length from 30 seconds to 300 seconds. Use the TcpTimedWaitDelay registry parameter to change this value. To do this, start Registry Editor (Regedt32.exe), locate the following key in the registry, and then modify the value HKEY_LOCAL_MACHINE\System\CurrectControlSet\services\Tcpip\Parameters (this parameter determines the length of time that a connection stays in the TIME_WAIT state when the connection is being closed. While a connection is in the TIME_WAIT state, the socket pair cannot be reused. For more information, see RFC 793): 
    Value Name:TcpTimedWaitDelay 
    Value Type: REG_DWORD-time in seconds 
    Valid Range: 30-300 (decimal) 
    Default: 0xF0 (240 decimal) 
    More information available at http://support.microsoft.com/default.aspx?scid=kb;EN-US;319502 .
      

  8.   

    hanbb 你好这是我的连接处理
      private int insert(String sql) throws SQLException, ParselException {
        com.hs.util.log.CommonLog.logger.info("sql: " + sql);
        int res = 0;
        Connection conn = null;
        Statement s = null;
        try {
          conn = HSConnection.getConnection();
          s = conn.createStatement();
          res = s.executeUpdate(sql);
        }
        catch (SQLException e) {
          System.out.println("error in sqlServerDAO.insert,message:" + e);
          throw e;
        }
        catch (ParselException e) {
          System.out.println("error in sqlServerDAO.insert,message:" + e);
          throw e;
        }
        finally{
          try{
            s.close();
            if(conn!=null)
              conn.close();
            conn=null;
          }
          catch(Exception fe){
            System.out.println("error in sqlServerDAO.insert Close,message:" + fe);
          }
        }
        return res;
      }
    另外在有事务时是这样处理的
        try {
          UserTransaction ut = HSConnection.getUserTransaction();
          ut.begin();
          try {
            rt = new BillDAO().createBill(bill);(在此执行的是上面的插入方法)
            if (rt > 0) {
              ut.commit();
            }
            else {
              res = "添加发货单信息时出错!";
              ut.commit();
            }
          }
          catch (Exception e) {
            com.hs.util.log.CommonLog.logger.warning(res + e.toString());
            ut.rollback();
            res = "程序在执行过程中出错!";
          }
          ut=null;
        }
        catch (Exception e) {
          com.hs.util.log.CommonLog.logger.warning(res + e.toString());
          res = "连接数据库出错!";
        }
    请问有无问题,非常感谢!!!!
      

  9.   

    检查一下你的sp3是否真正的安装上了``不是点击exe就完事了。。 exe只是解压而已
      

  10.   

    最后查询数据库的Sysprocess发现的确是连接没有关掉。但并不是所有的都没关掉,只有起事务时才关不掉
    也就是在
         try { 
          UserTransaction ut = HSConnection.getUserTransaction(); 
          ut.begin(); 
          try { 
            rt = new BillDAO().createBill(bill);(在此执行的是上面的插入方法) 
            if (rt > 0) { 
              ut.commit(); 
            } 
            else { 
              res = "添加发货单信息时出错!"; 
              ut.commit(); 
            } 
          } 
          catch (Exception e) { 
            com.hs.util.log.CommonLog.logger.warning(res + e.toString()); 
            ut.rollback(); 
            res = "程序在执行过程中出错!"; 
          } 
          ut=null; 
        } 
        catch (Exception e) { 
          com.hs.util.log.CommonLog.logger.warning(res + e.toString()); 
          res = "连接数据库出错!"; 
        } 
    这过程执行完成后发现数据库的连接一直处于Sleeping状态,我猜想是连接池配置的问题
    请教高手如何在Tomcat5.0.28中配置JTA的连接池