session.connection() deprecated on Hibernate?
We need to be able to get the associated java.sql.Connection
of a hibernate session. No other connection will work, as this connection may be associated with a running transaction.
If session.connection() is now deprecated, how am I supposed to do that?
You now have to use the Work API:
session.doWork(
new Work() {
public void execute(Connection connection) throws SQLException
{
doSomething(connection);
}
}
);
Or, in Java 8+ :
session.doWork(connection -> doSomething(connection));
If
session.connect()
is now deprecated, how am I supposed to do that?
You have to use Session#doWork(Work)
and the Work
API, as mentioned in the Javadoc:
connection()
Deprecated. (scheduled for removal in 4.x). Replacement depends on need; for doing direct JDBC stuff usedoWork(org.hibernate.jdbc.Work)
; for opening a 'temporary Session' use (TBD).
You have some time before Hibernate 4.x but, well, using a deprecated API somehow looks like this:
:)
Update: According to RE: [hibernate-dev] Connection proxying on the hibernate-dev list, it seems that the initial intention of the deprecation was to discourage the use of Session#connection()
because it was/is considered as a "bad" API, but it was supposed to stay at that time. I guess they changed their mind...
Try This
((SessionImpl)getSession()).connection()
Actuly getSession returns Session Interface type, you should see what is the original class for the session, type cast to the original class then get the connection.
GOOD LUCK!