1

In my java code, I am processing huge amount of data. So I moved the code as servlet to Cron Job of App Engine. Some days it works fine. After the amount of the data increases, the cron job is not working and shows the following error message.

2012-09-26 04:18:40.627
'ServletName' 'MethodName': Inside SQLExceptionjava.sql.SQLRecoverableException: 
    Connection is already in use.

I 2012-09-26 04:18:40.741
This request caused a new process to be started for your application, and thus caused 
your application code to be loaded for the first time. This request may thus take 
longer and use more CPU than a typical request for your application.

W 2012-09-26 04:18:40.741
A problem was encountered with the process that handled this request, causing it to 
exit. This is likely to cause a new process to be used for the next request to your 
application. If you see this message frequently, you may be throwing exceptions during 
the initialization of your application. (Error code 104)

How to handle this problem?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Master Mind
  • 2,386
  • 3
  • 31
  • 48

1 Answers1

0

This exception is typical when a single connection is shared between multiple threads. This will in turn happen when your code does not follow the standard JDBC idiom of acquiring and closing the DB resources in the shortest possible scope in the very same try-finally block like so:

public Entity find(Long id) throws SQLException {
    Connection connection = null; 
    // ...

    try {
        connection = dataSource.getConnection();
        // ...
    } finally {
        // ...
        if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
    }

    return entity;
}

Your comment on the question,

@TejasArjun i used connection pooling with servlet Init() method.

doesn't give me the impression that you're doing it the right way. This suggests that you're obtaining a DB connection in servlet's init() method and reusing the same one across all HTTP requests in all HTTP sessions. This is absolutely not right. A servlet instance is created/initialized only once during webapp's startup and reused throughout the entire remaining of the application's lifetime. This at least confirms the exception you're facing.

Just rewrite your JDBC code according the standard try-finally idiom as demonstrated above and you should be all set.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555