Apache Cayenne > Index > Cayenne FAQ > Reconnect
Sometimes an application would start giving "connection was lost" errors after long idle periods. There can be a few reasons for this error: some database servers will automatically drop idle client connections, database server may have been restarted, etc. Pooled connections may become invalid over a period of time due to one of these reasons, resulting in exceptions thrown when such connections are used.
Cayenne's default connection pool implements a few checks for invalid connections, and reconnects if possible. Unfortunately due to driver differences, this does not work reliably across database engines (One database were reconnects work rather well is Oracle). If you are still having this problem, here is a few suggested solutions:
- If you are using MySQL, MySQL JDBC driver offers "auto-reconnect" feature. Use a URL parameter "autoReconnect" set to "true" to enable reconnection. E.g.: jdbc:mysql://localhost/test?autoReconnect=true. Other database drivers may or may not have a similar feature.
- Configure your database server to prevent idle connections from being automatically closed. Consult your database documentation on the details.
- Use commons-dbcp connection pool. It can be used with Cayenne prior to 1.2, e.g. by mapping a pool via JNDI. In 1.2 Cayenne provides a DBCPDataSourceFactory to help with DBCP integration in standalone applications.