views:

74

answers:

2

I'm trying to figure out how to configure my project such that JPA will timeout and throw an exception after a configured amount of time. There are two situations where I would like this to happen:

  • When JPA is unable to even connect to the database
  • When a JPA query takes longer than the timeout threshold to return a result set

I'm not sure if these two scenarios can be configured separately (a different timeout threshold for each), or if one threshold is used for both.

My project is currently set up as follows:

  • Coding to the JPA 2.0 specification
  • Using Hibernate 3.5.6 as the JPA implementation
  • Using c3p0 connection pooling with Hibernate
  • Using persistence.xml configuration file (using Hibernate-specific property values only when necessary)
  • NOT using any Hibernate-specific configuration files
+1  A: 

This page on the Hibernate wiki details how to configure the c3p0 connection pool, including the timeout settings.

Note that these type of details really don't have much to do with JPA or Hibernate, but are settings that you set on the DataSource / database connection (c3p0 in this case) itself.

matt b
@mattb So this seems to answer my question about the connection timeout, but I still don't understand how I would set a timeout for a query. When you say "DataSource", are you referring to something at the JDBC layer? How would I access that from JPA?
Jim Tough
Correct me if I am wrong but C3P0 will just pool connections, and you still need to configure the underlying datasource - is this right? If so what I am referring to is that you need to configure the underlying JDBC driver for timeouts, both to acquire a connection and for individual queries
matt b
The only connection properties that I've needed to set are the driver class (oracle.jdbc.OracleDriver), the connection URL (jdbc:oracle:thin:@myhost:1521:ORCL) and the user and password. Those are all done via 4 standard JPA 2 properties in the persistence.xml file. I wouldn't know where to go if I wanted to try configuring JDBC stuff.
Jim Tough
+2  A: 

JPA2 persistence property "javax.persistence.query.timeout" allows you to set the timeout for a query (assuming the underlying JDBC driver supports it).

DataNucleus
@DataNucleus +1 for the heads-up on this property. Unfortunately it doesn't seem to work with the Oracle JDBC driver that comes in the ojdbc14.jar file. I can't get a newer driver yet because the project is using an Oracle 10g server and will not be upgrading for a while.
Jim Tough