我现在用连接池连接数据库,用tomcat做本地服务器一切正常,但是上传正式服务器之后,隔几天不用,项目就不能进行数据库操作了。重启一下服务器或者重新上传项目文件到服务器又可以正常使用了,不知道这是什么原因!

解决方案 »

  1.   

    看你说的情况很像数据库连接没有关闭,导致连接池满贴你的数据库连接的代码,如果是Hibernate,贴配置连接池的xml
      

  2.   

    public class ConnectionFactory {
    private static Properties prop = new Properties();
    /** 数据源 */
    private static DataSource ds = null; 

    //用来把Connection绑定到当前线程上的变量
    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); static{
    try {
    prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("jdbc.properties"));
    } catch (IOException e) {
    e.printStackTrace();
    System.out.println("在classpath下没有找到jdbc.properties文件");
    }

    //使用C3P0连接池技术
    try {
    Class.forName("com.mysql.jdbc.Driver");

    DataSource unpooled = DataSources.unpooledDataSource(
    prop.getProperty("url"),
    prop.getProperty("user"),
    prop.getProperty("password"));
    ds = DataSources.pooledDataSource(unpooled);

    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    }

    }

    /**
     * 根据数据库的默认连接参数获取数据库的Connection对象
     * @return 成功,返回Connection对象,否则返回null
     */
    public static synchronized Connection getConnection(){
    Connection conn = tl.get(); //先从当前线程上取出连接实例

    if(null == conn){ //如果当前线程上没有Connection的实例 
    try {
    conn = ds.getConnection(); // 从连接池中取出一个连接实例 
    tl.set(conn);  //把它绑定到当前线程上
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    return conn;
    }
    /**
     * 获取事务管理器
     * @return
     */
    public static synchronized TransactionManager getTranManager(){
    return new TransactionManager(getConnection());
    }

    protected static void close(Connection conn) throws DaoException{
    if(conn != null){
    try {
    conn.close();
    tl.remove();  //卸载线程绑定
    } catch (SQLException e) {
    throw new DaoException("关闭连接时出现异常",e);
    }
    }
    }

    }
      

  3.   

    public class DbUtils { private static Properties properties=new Properties();

    private static DataSource ds=null;
    /** 线程局部化的Connection */
    private static ThreadLocal<Connection> t1=new ThreadLocal<Connection>();

    static{
    try{
    properties.load(Thread.currentThread().getContextClassLoader().
              getResourceAsStream("jdbc.properties"));
    Class.forName(properties.getProperty("driver"));
    ds=DataSources.unpooledDataSource(properties.getProperty("url"),properties.getProperty("user"),
    properties.getProperty("password"));
    ds=DataSources.pooledDataSource(ds);
    }catch (ClassNotFoundException e) {
    e.printStackTrace();
    System.out.println("加载数据库驱动类失败!!!");
    } catch (IOException e) {
    e.printStackTrace();
    System.out.println("在classpath下没有找到jdbc.properties文件");
    } catch (SQLException e) {
    e.printStackTrace();
    System.out.println("创建连接池失败!!!");
    }
    }

    private DbUtils() {
    }

    /**
     * 根据数据库的默认连接参数获取线程局部化的Connection对象
     * 
     * @return 返回当前线程上的Connection对象
     * @throws DaoException
     */
    public  static synchronized Connection getConnection()throws DaoException{
    Connection con=t1.get();
    if(con==null){
    try{
    con=ds.getConnection();
    t1.set(con);
    }catch(SQLException ce){
    throw new DaoException(ce);
    }
    }
    return con;
    }

    /**
     * 获取事务管理器
     * @return
     */
    public static synchronized TransactionManager getTranManager(){
    return new TransactionManager(getConnection());
    }

    protected static void close(Connection conn) throws DaoException{
    if(conn != null){
    try {
    conn.close();
    t1.remove();  //卸装线程绑定
    } catch (SQLException e) {
    throw new DaoException("关闭连接时出现异常",e);
    }
    }


    } public static String getURL(String s){
            String s1 = "";
            try{
                URL url = new URL(s);
                DataInputStream datainputstream = new DataInputStream(url.openConnection().getInputStream());
                ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
                try{
                    byte abyte0[] = new byte[1024];
                    boolean flag = false;
                    do{
                        int i = datainputstream.read(abyte0);
                        if(i == -1)
                            break;
                        bytearrayoutputstream.write(abyte0, 0, i);
                    } while(true);
                    bytearrayoutputstream.flush();
                    byte abyte1[] = bytearrayoutputstream.toByteArray();
                    s1 = new String(abyte1, 0, abyte1.length, "GB2312");
                }catch(Exception exception1){
                    exception1.printStackTrace();
                }finally{
                    datainputstream.close();
                    bytearrayoutputstream.close();
                }
            }catch(Exception exception) { }
            return s1;
        }
    }
      

  4.   

    呵呵,这个跟程序无关,是mysql的问题,楼主可以在连的时候,多尝试几次连接。它就连上了,楼主可以去看看mysql的相关资料,因为几个小时或者一定时间不连接,他就会断开的。最简单的就是楼主程序在操作数据库之前,多进行几次链接测试,链接上了,才进行操作,否则继续链接。当然要判断一下了,操作次数过多,直接给予提示。一般是没有问题的。