There's a misunderstanding here: A SessionFactory will not hold database connections on startup. Rather, each Session will fetch a connection from an underlying JDBC connection pool on opening, returning it to the pool on closing. You don't need to reconnect the SessionFactory at any time: It will automatically fetch fresh connections from the pool for each new Session.
So the problem of holding open connections is purely an issue of the JDBC connection pools, just like with plain JDBC. In a typical Spring app, JDBC DataSources will serve as pools, either locally defined or fetched from JNDI. Each LocalSessionFactoryBean receives a reference to its DataSource via the "dataSource" bean property. From the DAO point of view, you should assume that a pool always returns valid connections.
To deal with database outages, you'd need to use a reliable JDBC connection pool, possibly issuing validation queries before it returns a connection to the application. With proper pool configuration, the pool should automatically retry if a connection is dead, throwing an exception only if the retry failed too. You should not need to do that in your DAOs, neither with plain JDBC nor with Hibernate.
If you're running in an app server, try to configure the container DataSources accordingly, linking them into a Spring application context via JndiObjectFactoryBeans. Else, you could use Jakarta's Commons DBCP 1.1, or C3PO for local DataSource definitions. In any case, do your own tests with a couple of pools, and never trust the feature lists :-)
Juergen
|