Announcement Announcement Module
Collapse
No announcement yet.
problem with c3p0 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • problem with c3p0

    hello, my friend, I encounter a strange problem related to c3p0.
    my code is :
    =============================
    public class Cp30ConnectionManager {
    private static Cp30ConnectionManager instance;
    private ComboPooledDataSource ds;

    private Cp30ConnectionManager() throws Exception {
    ds = new ComboPooledDataSource();
    ds.setDriverClass("com.mysql.jdbc.Driver");
    ds.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/user");
    ds.setUser("root");
    ds.setPassword("123");
    ds.setInitialPoolSize(2);
    ds.setMaxPoolSize(3);
    ds.setMinPoolSize(1);
    ds.setAcquireIncrement(5);
    ds.setIdleConnectionTestPeriod(60);
    ds.setAcquireRetryAttempts(30);
    ds.setAcquireRetryDelay(1000);
    ds.setBreakAfterAcquireFailure(true);
    }


    public static final Cp30ConnectionManager getInstance() {
    if (instance == null) {
    try {
    instance = new Cp30ConnectionManager();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    return instance;
    }

    public synchronized final Connection getConnection() {
    try {
    return ds.getConnection();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    return null;
    }

    protected void finalize() throws Throwable {
    DataSources.destroy(ds);
    super.finalize();
    }
    }


    public class TestCp30ConnectionManager implements Runnable{
    private static Cp30ConnectionManager cm = Cp30ConnectionManager.getInstance();

    public void run() {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    String sql = "select * from user";
    for (int i = 0; i < 3; i++) {
    try {
    conn = cm.getConnection();
    System.out.println("connection["+i+"]:"+conn); //print the connection object
    stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
    rs.next();
    } catch (Exception ex) {
    ex.printStackTrace();
    } finally {
    if (conn != null) {
    try {
    conn.close();
    } catch (Exception e) {
    }
    }
    }

    try {
    System.out.println("datasource["+i+"]:"+cm.getDs());
    Thread.sleep(1000);
    } catch (Exception e) {

    }

    }
    }

    public static void main(String[] args) {
    new Thread(new TestCp30ConnectionManager()).run();
    new Thread(new TestCp30ConnectionManager()).run();
    new Thread(new TestCp30ConnectionManager()).run();
    }
    }
    =============================
    when I run the TestCp30ConnectionManager.main(), I found the connection object is always different each time. but I think some connections somehow may be used many times, but the result is not, why?? help me

  • #2
    Hi,

    I haven't looked at your code in detail, but I'm pretty sure you're mean to call start() instead of run() to set your Threads into action

    --
    Stephen

    Comment

    Working...
    X