为什么红色的出现了多次呢??是不是应该只出现一次啊???
请大家帮我分析一下既然是初始化怎么会出现很多次呢?011-04-05 19:55:41,744 INFO  开始定制!
2011-04-05 19:55:41,744 INFO  Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource@1cbf926[ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, description -> null, driverClass -> com.microsoft.jdbc.sqlserver.SQLServerDriver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1cbf926, idleConnectionTestPeriod -> -1, initialPoolSize -> 3, jdbcUrl -> jdbc:microsoft:sqlserver218.201.15.66:1433;databasename=healthinfo120, loginTimeout -> 0, maxIdleTime -> 0, maxPoolSize -> 100, maxStatements -> 180, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectiveProxies -> false ]2011-04-05 19:55:41,760 INFO  已经存在用户信息但是用户需要暂停业务现在更新.
2011-04-05 19:55:41,775 INFO  Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource@eacdfe[ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, description -> null, driverClass -> com.microsoft.jdbc.sqlserver.SQLServerDriver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> eacdfe, idleConnectionTestPeriod -> -1, initialPoolSize -> 3, jdbcUrl -> jdbc:microsoft:sqlserver://218.201.15.66:1433;databasename=healthinfo120, loginTimeout -> 0, maxIdleTime -> 0, maxPoolSize -> 100, maxStatements -> 180, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectiveProxies -> false ]2011-04-05 19:55:41,775 INFO  已经存在用户信息但是用户需要暂停业务现在更新.
2011-04-05 19:55:41,775 INFO  update BuserInfo set user_fwzt=4 where User_mobi='15969762880' 
2011-04-05 19:55:41,775 INFO  因为这不是一个开通服务的请求所以该激活的激活,该暂停业务的暂停,该取消的取消. 操作成功
2011-04-05 19:55:41,775 INFO  返回SOAP响应信息完毕。本次请求任务结束!等待下一个请求...........
2011-04-05 20:09:12,057 INFO  检测到新的请求2011-4-5 20:09:12》》》开始分析请求数据包.
2011-04-05 20:09:12,072 INFO  将请求的SOAP 转为DocumentOK!开始解析数据包......
2011-04-05 20:09:12,072 INFO  解析数据包详细数据完毕!
2011-04-05 20:09:12,072 INFO  这是一激活-激活-激活-激活-服务的请求!
2011-04-05 20:09:12,072 INFO  开始定制!
2011-04-05 20:09:12,072 INFO  Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource@1482580[ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, description -> null, driverClass -> com.microsoft.jdbc.sqlserver.SQLServerDriver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1482580, idleConnectionTestPeriod -> -1, initialPoolSize -> 3, jdbcUrl -> jdbc:microsoft:sqlserver://218.201.15.66:1433;databasename=healthinfo120, loginTimeout -> 0, maxIdleTime -> 0, maxPoolSize -> 100, maxStatements -> 180, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 300, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, usesTraditionalReflectiveProxies -> false ]2011-04-05 20:09:12,072 INFO  已经存在用户信息但是用户需要激活业务.
为什么红色的出现了多次呢??是不是应该只出现一次啊???
请大家帮我分析一下,下面是我的代码
package zh.c3p0;import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;import com.mchange.v2.c3p0.ComboPooledDataSource;public class DBConn {
private static Connection conn = null;
private Statement stat = null;
private ResultSet rs = null; static
{ conn = getConnectionC3po();
} public DBConn() { }
public static Connection getConnectionC3po()
{
try
{

ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass("com.microsoft.jdbc.sqlserver.SQLServerDriver");
cpds
.setJdbcUrl("jdbc:microsoft:sqlserver://218.201.66.66:1433;databasename=xxx");
cpds.setUser("xx");
cpds.setPassword("xxx");
cpds.setMaxStatements(180);
cpds.setMaxPoolSize(100);
conn = cpds.getConnection();
return conn;
} catch (Exception e)
{
e.printStackTrace();
System.out.println("获得连接出现错误了!!!!!!!!!!"); return null;
} } public ResultSet executeQuery(String strSQL)
{
try
{ stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
this.rs = this.stat.executeQuery(strSQL); return this.rs;
} catch (Exception e)
{
e.printStackTrace();
}
return this.rs;
} public int executeUpdate(String strSQL)
{
int c = 0;
try
{ this.stat = this.conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
c = this.stat.executeUpdate(strSQL); } catch (Exception e)
{
c = 0;
e.printStackTrace();
}
return c;
} // 事务处理
public boolean public_shiWuChuLi(ArrayList sqlarr) throws SQLException,
PropertyVetoException
{ this.stat = this.conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
try
{
conn.setAutoCommit(false);// 更改JDBC事务的默认提交方式
for (int i = 0; i < sqlarr.size(); i++)
{
stat.executeUpdate(sqlarr.get(i).toString());
}
conn.commit();// 提交JDBC事务
conn.setAutoCommit(true);// 恢复JDBC事务的默认提交方式 return true;
} catch (Exception exc)
{
conn.rollback();// 回滚JDBC事务
exc.printStackTrace();
return false;
} finally
{
close();
}
} public void close()
{
try
{
if (this.rs != null)
{
this.rs.close();
}
if (this.stat != null)
{
this.stat.close();
}
if (this.conn != null)
{
this.conn.close();
} } catch (SQLException e)
{
e.printStackTrace();
}
} public static void main(String[] args)
{ DBConn db = new DBConn();
Statement stmt = null;
ResultSet rs = null;
try
{ stmt = conn.createStatement();
rs = stmt.executeQuery("select * from aguojia");
while (rs.next())
{
System.out.println(rs.getString(2));
} } catch (SQLException e)
{
e.printStackTrace();
} finally
{ } }}
请大家帮我看看这个类是不是写的不对劲啊

解决方案 »

  1.   

    问题:1:DataSource 只需要在启动时初始化一次就够了,并不需要你每次 getConnection 时去实例化一次
    2:一个我说了三百年的问题,但就是没人听,再大字标红一次!
    严禁将 JDBC 的连接对象 Connection, Statement, ResultSet 弄成成员变量!
    把这些对象弄成成员变量是可以的,但是需要有并发编程相当的功底。3:JDBC 的代码应该这样写,JDBC 基本上没有什么投机取巧的办法。
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        Connection con = xxxx.getConnection();
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, xxx);
        ps.setInt(2, aaa);
        rs = ps.executeQuery();
        while(rs.next()) {
          rs.xxxx;
        }
    } catch (SQLException e) {
        e.....
    } finally {
        if(rs != null) try{ rs.close(); } catch (SQLException e){ e..... }
        if(ps != null) try{ ps.close(); } catch (SQLException e){ e..... }
        if(con != null) try{ con.close(); } catch (SQLException e){ e..... }
    }