1

In my java hibernate application I am getting the following error message, although the application continues to run after the message is displayed:

o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 17008, SQLState: 99999
o.h.e.jdbc.spi.SqlExceptionHelper - Closed Connection

If this occurs I want the application to stop.

I have implemented the following but it is not stopping the application if this "closed connection" occurs:

Current Code:

  try {
               personName = findPersonNameById(id); //hibernate query method
            } catch (Exception e) {
                throw new ClosedConnectionException("Closed connection error");
            }

Custom exception class:

public class ClosedConnectionException extends RuntimeException {
public ClosedConnectionException(String message) {
    super(message);
}
}

For reference, here is my persistence.xml C3PO values, could something here be causing the connection to close? :

 <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.connection.provider_class" value="org.hibernate.c3p0.internal.C3P0ConnectionProvider"/>
            <property name="hibernate.c3p0.max_size" value="1"/>
            <property name="hibernate.c3p0.min_size" value="1"/>
            <property name="hibernate.c3p0.acquire_increment" value="2"/>
            <property name="hibernate.c3p0.idle_test_period" value="300"/>
            <property name="hibernate.c3p0.max_statements" value="15"/>
            <property name="hibernate.c3p0.timeout" value="0"/>
            <property name="hibernate.c3p0.unreturnedConnectionTimeout" value="30000"/>
            <property name="hibernate.c3p0.dataSourceName" value="My connection"/>
            <property name="hibernate.jdbc.fetch_size" value="50"/>
            <property name="hibernate.default_batch_fetch_size" value="10"/>
        </properties>
java123999
  • 6,974
  • 36
  • 77
  • 121

2 Answers2

1

You should rather check for state of the connection and then accordingly call or execute your query. There is isClosed() method which tells you whether the connection is in closed state and you can probably use the same. Something like below probably assuming your connection object name is conn

if(!conn.isClosed())
    personName = findPersonNameById(id);

Also, see this other post Java JDBC connection status

Community
  • 1
  • 1
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Thank you, Is there a way that I can get this connection object from the JPAPersistenceManager? The code I have shown is in a service layer and I cannot see the connection in order to check it? – java123999 Jan 12 '17 at 14:54
0

Do you catch the ClosedConnectionException in the calling code? - If the answer is yes, what's done to handle the exception? - If the answer is no, then your code will run until it's caught elsewhere because it's a runtime exception. - It's little odd to encounter a closed connection to a database. In my experience it's a rare exception. There could be a serious hidden issue.

Velusamy Velu
  • 372
  • 1
  • 14
  • Yes it is caught in a different block, but my issue is that it is not causing the application to stop as it should – java123999 Jan 12 '17 at 14:50
  • When it's caught in a different block what action is taken in that `catch {}` block? Is the application stopped programmatically or you let it to continue to run? If you could share that `catch {}` block, the problem may become apparent. – Velusamy Velu Jan 12 '17 at 15:03