Hi all,
First off, I absolutely think Hibernate is a godsend. Thank you to all the developers who have made it possible.
I've been developing and maintaining a servlet based webapp for about 5 years now, and have decided that moving to hibernate will significantly improve performance and maintainability into the future. I have things working, but I wanted to get feedback on whether what I'm doing is totally unacceptable, or if it makes some sense. Basically there's a single contact point for retrieving JDBC connections in the application, which I have changed to this (consider it pseudo-code, thread synchronization and such have been thoroughly tested):
Code:
class WhatEver {
Connection getConnection() {
SessionFactory factory = ........;
Session session = factory.getCurrentSession();
session.beginTransaction();
Connection con = session.connection();
return con;
}
void returnConnection(Connection con) {
factory.getCurrentSession().close();
}
}
I have this in hibernate.cfg.xml, so as long as a connection is only used by one thread (which I've spent a significant amount of effort verifying is the case), I think it should make sense...
Code:
<property name="current_session_context_class">thread</property>
The code would be called as so:
Code:
Connection con = whatever.getConnection();
try {
PreparedStatement ps = con.prepareStatement("SELECT * FROM MEH;");
ResultSet rs = ps.executeQuery();
blahblah(rs);
} finally {
rs.close();
ps.close();
whatever.returnConnection(con);
}
Is there a preferred way of using raw JDBC connections for the pieces of my application I cannot afford the time to port immediately? Does this make sense?
Thanks much for any feedback, I look forward to a great relationship with Hibernate :)
Ben.