views:

66

answers:

2

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?

Thanks,

-Sergio

+3  A: 

You now have to use the Work API:

session.doWork(
    new Work() {
        public void execute(Connection connection) throws SQLException 
        { 
            doSomething(connection); 
        }
    }
);

However, I find this cumbersome at best, and would still use session.connection(). The choice is up to you.

Samuel_xL
I don't like using deprecated stuff but i guess this is a good reason to start using it. But I did not know about the Work API. Thanks very much.
Sergio Oliveira Jr.
Wow. I am using Hibernate 3.2.7.ga but my org.hibernate.Session does NOT have any doWork method. That's great!
Sergio Oliveira Jr.
+2  A: 

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 use doWork(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 that:

alt text:)

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...

Pascal Thivent
+1: Love the image!
Don Roby
My Javadoc here is slightly different than yours. Just a little less clear: To be replaced with a SPI for performing work against the connection; scheduled for removal in 4.x. Your JavaDoc says it all. This JavaDoc says nothing.
Sergio Oliveira Jr.
@Sergio Indeed. But if I may, you should mention important things like the Hibernate version you're using in your question. Your version is pretty old (the javadoc of [`Session#connection()`](http://docs.jboss.org/hibernate/core/3.3/api/org/hibernate/Session.html#connection%28%29) in Hibernate Core 3.3 mentions the alternative) and that's typically something readers can't guess.
Pascal Thivent
@Pascal Version 3.2.7.ga is the latest I could find on Maven. GroupId = org.hibernate and artifactId = hibernate. I wonder if Maven can provide the latest version or if you just need to copy the jar and ignore maven.
Sergio Oliveira Jr.
@Sergio That's because you're using the old monolithic jar (`hibernate`) and not [`hibernate-core`](http://repo1.maven.org/maven2/org/hibernate/hibernate-core/) which has more recent versions. And for ultimate versions (3.5.x), they are in available in the [JBoss Nexus repository](http://stackoverflow.com/questions/2999077/how-to-compile-hibernate-project-with-maven).
Pascal Thivent
@Pascal Thanks! One big problem is that I need to pass the connection around, so if Hibernate will not be able to provide me with its connection then it will be bad. I will need to get this connection some way else. I think whoever had this idea of deprecating the connection method should think again.
Sergio Oliveira Jr.
@Sergio I somehow agree and even if they deprecate it to discourage its use (like the `java.util.Date` constructor) they should at least not remove it (as initially planned). Maybe send a mail on the developer list.
Pascal Thivent