0

all.

I can't speak eng well.. sorry.

I have a problem for DB Connection in java program.

First code is getting each connection for each query.

Second code is just using one connection for every query.

Here is the sample codes.

Example #1

public void func() {
    Connection conn = null;
    try {
        conn = getConnection();

        //Just example.. 
        //Here, it is assumed that it has been successfully executed.
        String value1 = selectValue1(conn); 
        conn.close();

        conn = getConnection();
        String value2 = selectValue(conn); 
        conn.close();

    } catch(Exception e) {
        ....
    } finally {
        try { 
            if (conn != null) 
                conn.close();
        } catch(Exception e2) { }
    }
}

Example #2

public void func() {
    Connection conn = null;
    try {
        conn = getConnection();

        //Just example.. 
        //Here, it is assumed that it has been successfully executed.
        String value1 = selectValue1(conn); 
        String value2 = selectValue2(conn); 

    } catch(Exception e) {
        ....
    } finally {
        try { 
            if (conn != null) 
                conn.close();
        } catch(Exception e) { }
    }
}

Both are functionally executed.

But, when I perform the performance testing, occurred problems often. (There are about 10 classes that performs a similar code.)

**java.sql.SQLException: Connection does not exist**
    at Altibase.jdbc.driver.ex.exception(ex.java:52) ~[Altibase.jar:na]
    at Altibase.jdbc.driver.ex.exception(ex.java:37) ~[Altibase.jar:na]
    at Altibase.jdbc.driver.cmnTCP.send(cmnTCP.java:318) ~[Altibase.jar:na]
    at Altibase.jdbc.driver.cmp.flush(cmp.java:252) ~[Altibase.jar:na]
    at Altibase.jdbc.driver.cmp.writePrepareProtocol(cmp.java:1590) ~[Altibase.jar:na]
    at Altibase.jdbc.driver.ABConnection.prepare(ABConnection.java:1002) ~[Altibase.jar:na]
    at Altibase.jdbc.driver.ABPreparedStatement.<init>(ABPreparedStatement.java:62) ~[Altibase.jar:na]
    at Altibase.jdbc.driver.ABConnection.prepareStatement(ABConnection.java:1060) ~[Altibase.jar:na]
    at Altibase.jdbc.driver.ABConnection.prepareStatement(ABConnection.java:1055) ~[Altibase.jar:na]
    at Altibase.jdbc.driver.ABConnection.prepareStatement(ABConnection.java:1033) ~[Altibase.jar:na]
    at net.herit.ini.dbpool.PooledConnection.prepareStatement(PooledConnection.java:138) ~[slee.jar:na]

How can I fix this problem? or What's difference ? &-|

thx.

J-Hyun
  • 13
  • 2
  • 1
    Your first example calls `close()` at least once too often, because you have a call in the `finally` block; and two calls in the body. – Elliott Frisch Sep 20 '16 at 02:13
  • thx to comment. I have another question. Do I close the already closed connection ? In this case, does the error occur? – J-Hyun Sep 20 '16 at 02:41
  • Why don't you change it and see? I can't reproduce your problem, I'm not even sure which connection pool you're using. – Elliott Frisch Sep 20 '16 at 02:55
  • What does `getConnection()` *do*? and `selectValue1()`? and `selectValue2()`? Hard to see how anybody can answer this untill you you tell us. – user207421 Sep 20 '16 at 08:35
  • [see this](http://www.codeproject.com/Articles/662248/Difference-between-and-and) to check difference between `&` and `&&`, `|` and `||` operator. – yash Sep 20 '16 at 09:08

0 Answers0