请教一个resin2.1.6频繁死掉的问题

08-01-04 ybzshizds
公式的网站是jsp+servlet+javabean一个不是很规范的mvc模式.web服务器和数据库服务器是分开的.现在resin总是死掉(2天一次,很有规律的),一直找不到原因,特此寻求帮助.

这是后台经常抛的异常:

1、java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Connection reset by peer: socket write error

2、[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.

3、java.net.SocketException: Connection reset

4、java.net.SocketException: No buffer space available (maximum connections   reached?): JVM_Bind

我怀疑是数据库连接池写的有问题:

   

ybzshizds
2008-01-04 09:38
// DBConnection.java类

public final class DBConnection { 
private static int databaseType = 0; 
private static int maxTimeToWait = 2000; // 2 秒钟 
private static int minutesBetweenRefresh = 30; // 30 分钟 
private static ConnectionManager connectionManager = null; 
private static DataSource dataSource = null; 
private static long lastGetConnectionTime = 0; 
private static long lastCloseAllConnectionsTime = 0; 

private static Connection conn = null; 
private static Statement stmt = null; 
private static ResultSet rs = null; 
private static PreparedStatement ptmt = null; 

private DBConnection() { // so cannot new an instance 
} 
static { 
option o = new option(); 
databaseType = o.databaseType; 
maxTimeToWait = o.maxTimeToWait; 
minutesBetweenRefresh = o.minutesBetweenRefresh; 
connectionManager = ConnectionManager.getInstance(o); 
} 
public static Connection getConnection() throws SQLException { 
long now = System.currentTimeMillis(); 
lastGetConnectionTime = now; 
if (now - lastCloseAllConnectionsTime > 
DateUtil.MINUTE * minutesBetweenRefresh) { 
boolean isBalance = closeAllConnections(); 
if (isBalance == false) { 
try { 
Thread.sleep(2000); 
} 
catch (Exception ex) {} 
} 
} 
conn = connectionManager.getConnection(maxTimeToWait); 
if (conn == null) { 
throw new SQLException("DBConnection: 不能从连接池中获得数据库连接!"); 
} 
return conn; 
} 
//调用DBConnection.closeAllConnections来关闭所有的数据库连接 
public static boolean closeAllConnections() { 
boolean retValue = true; // balance (default) 
lastCloseAllConnectionsTime = System.currentTimeMillis(); 
if (connectionManager != null) { 
retValue = connectionManager.release(); 
} 
else { 
} 
return retValue; 
} 
public static void closeConnection(Connection myconn) { 
if (myconn == null) { 
return; 
} 
connectionManager.freeConnection(myconn); 
} 
public static void resetStatement() { 
if (stmt != null) { 
try { 
stmt.setMaxRows(0); 
} 
catch (SQLException e) { 
} 
try { 
stmt.setFetchSize(0); 
} 
catch (SQLException sqle) { 
} 
} 
} 
public static void closeStatement(Statement mystmt) { 
try { 
if (mystmt != null) { 
mystmt.close(); 
} 
} 
catch (SQLException e) { 
} 
} 
public static void closeResultSet(ResultSet myrs) { 
try { 
if (myrs != null) { 
myrs.close(); 
} 
} 
catch (SQLException e) { 
} 
} 
public static void closePtmt(PreparedStatement myptmt) { 
try { 
if (myptmt != null) { 
myptmt.close(); 
} 
} 
catch (SQLException e) { 
} 
} 
public static void closePtmt(CallableStatement proc) { 
try { 
if (proc != null) { 
proc.close(); 
} 
} 
catch (SQLException e) { 
} 
} 
} 
<p>

[该贴被ybzshizds于2008-01-04 09:42修改过]

[该贴被ybzshizds于2008-01-04 09:47修改过]

ybzshizds
2008-01-04 09:49

package com.jetsum.db;
import java.sql.*;
import java.util.Vector;
import java.util.Enumeration;
class ConnectionManager {
  private static final int TIME_BETWEEN_RETRIES = 500; // O.5 second
  static private ConnectionManager init = null;

  private DBConnectionPool pool = null; //连接池

  /**
   * 私有构造函数:从数据库配置文件从得到数据库信息,以及配置信息
   * @param option DBOptions:数据库连接参数
   */
  private ConnectionManager(option opt) {
    try {
      Class.forName(opt.driverClassName).newInstance();
    }
    catch (Exception e) {
    }
    pool = new DBConnectionPool(opt.databaseURL, opt.databaseUser,
                                opt.databasePassword, opt.maxConnection);
    System.out.println(opt.databaseURL+""+opt.databaseUser+""+opt.databasePassword+""+opt.maxConnection);
  }

  /**
   * 当这个方法第一次被调用的时候得到一个单独的实例
   * @return DBConnectionManager The single instance.
   */
  static synchronized public ConnectionManager getInstance() {
    if (init == null) {
      option opt = new option();
      init = new ConnectionManager(opt);
    }
    return init;
  }

  /**
   * 当这个方法第一次被调用的时候得到一个单独的实例
   * @return DBConnectionManager The single instance.
   */
  static synchronized public ConnectionManager getInstance(option opt) {
    if (init == null) {
      if (opt == null) {
        opt = new option();
      }
      init = new ConnectionManager(opt);
    }
    return init;
  }

  /**
   * 从数据库连接池中得到一个连接
   * @param con The Connection
   */
  void freeConnection(Connection con) {
    pool.freeConnection(con);
  }

  /**
   * 当数据库连接池中定义的最大连接数还没有达到的时候,创建一个连接
   *
   * @return Connection The connection or null
   */
  Connection getConnection() {
    return pool.getConnection();
  }
  /**
   * 如果没有可用的连接,而且连接数没有达到最大时创建一个连接,
   * 如果连接数达到最大则,等待连接释放后在创建新的连接
   * @param time The number of milliseconds to wait
   * @return Connection The connection or null
   */
  Connection getConnection(long time) {
    return pool.getConnection(time);
  }

  /**
   * 关闭所有连接
   * @return true 如果数据库连接池是空的或者有可以使用的连接
   *         false 如果连接数超过连接池允许的连接数
   */
  boolean release() {
    return pool.release();
  }

  //内部连接池类
  class DBConnectionPool {
    private int checkedOut = 0;
    private Vector freeConnections = new Vector(); //空闲的连接

    private int maxConn = 0;
    private String password = null;
    private String URL = null;
    private String user = null;

    /**
     * 创建一个新的连接池
     * @param URL  JDBC 驱动的地址
     * @param user 数据库用户名
     * @param password 数据库密码
     * @param maxConn 最大的连接数
     */
    public DBConnectionPool(String URL, String user, String password,
                            int maxConn) {
      this.URL = URL;
      this.user = user;
      this.password = password;
      this.maxConn = maxConn;
    }

    
<p>

ybzshizds
2008-01-04 09:50

// 这是接上帖的ConnectionManager.java类
   synchronized void freeConnection(Connection con) {

      if (con != null) {
        if (checkedOut <= 0) {
          // 这个表示连接数超过最大的连接数
          try {
            con.close();
          }
          catch (SQLException ex) {}
        }
        else {

          freeConnections.addElement(con);
          checkedOut--;
          notifyAll();
        }
      }
      //System.out.println("释放连接后为:----"+checkedOut);
     // System.out.println("");
    }

    synchronized Connection getConnection() {
      Connection con = null;
      while ( (freeConnections.size() > 0) && (con == null)) {

        con = (Connection) freeConnections.firstElement();
        freeConnections.removeElementAt(0);
        try {
          if (con.isClosed()) {
            con = null;
          }
        }
        catch (SQLException e) {
          con = null;
        }
      }

      if (con == null) {
        if (maxConn == 0 || checkedOut < maxConn) {
          con = newConnection();
        }
      }
      if (con != null) {
        checkedOut++;
      }
      //System.out.print("系统连接数量:-----"+checkedOut+"     ");
      return con;
    }

    Connection getConnection(long timeout) { //等待的时间内获取连接
      long startTime = System.currentTimeMillis(); //发出连接请求是,得到连接的时间
      Connection con;
      while ( (con = getConnection()) == null) {
        long elapsedTime = System.currentTimeMillis() - startTime;
        if (elapsedTime >= timeout) {
          return null;
        }

        long timeToWait = timeout - elapsedTime;
        if (timeToWait > TIME_BETWEEN_RETRIES) {
          timeToWait = TIME_BETWEEN_RETRIES;
        }
        try {
          Thread.sleep(timeToWait);
        }
        catch (InterruptedException e) {}
      }
      return con;
    }

    //同步
    synchronized boolean release() { //释放
      boolean retValue = true;
      Enumeration allConnections = freeConnections.elements();
      while (allConnections.hasMoreElements()) {
        Connection con = (Connection) allConnections.nextElement();
        try {
          con.close();
        }
        catch (SQLException e) {
        }
      }
      freeConnections.removeAllElements();
      if (checkedOut != 0) {
        retValue = false;
      }
      checkedOut = 0;
      return retValue;
    }

    private Connection newConnection() {
      Connection con = null;
      try {
        if (user == null) {
          con = DriverManager.getConnection(URL);
        }
        else {
          con = DriverManager.getConnection(URL, user, password);
        }
        con.setAutoCommit(true); //thread 804 by trulore
      }
      catch (SQLException e) {
        return null;
      }
      return con;
    }
  }
}
<p>

ybzshizds
2008-01-04 09:52

// 这是dao类一部分代码: 
   import java.sql.*; 
import java.util.Vector; 
import java.util.HashMap; 
import java.util.Calendar; 
import java.util.GregorianCalendar; 
import com.jetsum.db.*; 
public class peopleDao{ 
private Connection conn = null; 

private Statement stmt = null; 

private ResultSet rs = null; 

private PreparedStatement ptmt = null; 
private String sql; 
private int rowCount=-1; 

private int pageCount; 

private int length; 

private String pagestr; 
public peopleDao() { 
} 
    

 public int addpic(String id,String imagename){ 
GregorianCalendar gc=new GregorianCalendar(); 
String time=new java.text.SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime()); 

int i=0; 
String sql="select * from t_gr_zpgl where deng_ji_hao=?"; 
try{ 
conn = DBConnection.getConnection(); 
ptmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_READ_ONLY); 
ptmt.setString(1,id); 
rs=ptmt.executeQuery(); 
if(rs.next()){ 
sql="update t_gr_zpgl set que_ding=0 where deng_ji_hao=?"; 
try{ 
conn = DBConnection.getConnection(); 
ptmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_READ_ONLY); 
ptmt.setString(1,id); 
ptmt.executeUpdate(); 
} 
catch (Exception e) { 
System.out.println(e.toString()); 
} 

}else{ 
sql="insert into t_gr_zpgl(deng_ji_hao,que_ding) values(?,?)"; 
try{ 
conn = DBConnection.getConnection(); 
ptmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_READ_ONLY); 
ptmt.setString(1,id); 
ptmt.setInt(2,0); 
ptmt.executeUpdate(); 
} 
catch (Exception e) { 
System.out.println(e.toString()); 
} 

} 
} 

catch (Exception e) { 
System.out.println(e.toString()); 
} 

sql="update rcjbxx set img=?,last_modify_rq=? where deng_ji_hao=?"; 
try{ 
conn = DBConnection.getConnection(); 
ptmt = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, 
ResultSet.CONCUR_READ_ONLY); 
ptmt.setString(1,imagename); 
ptmt.setString(2,time); 
ptmt.setString(3,id); 
i=ptmt.executeUpdate(); 
} 
catch (Exception e) { 
System.out.println(e.toString()); 
} 

finally { 
DBConnection.closeResultSet(rs); 
DBConnection.closePtmt(ptmt); 
DBConnection.closeConnection(conn); 
} 
return i; 
} 

  }

猜你喜欢
2Go 1 2 下一页