HibernateJpaDialect borrows JDBC Connection on demand (supporting aggressive release; SPR-6895)

This commit is contained in:
Juergen Hoeller 2010-02-23 16:13:04 +00:00
parent 9e5b129ba5
commit ef227c5d01
1 changed files with 21 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,7 +28,7 @@ import org.hibernate.ejb.HibernateEntityManager;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.datasource.ConnectionHandle; import org.springframework.jdbc.datasource.ConnectionHandle;
import org.springframework.jdbc.datasource.SimpleConnectionHandle; import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.orm.hibernate3.SessionFactoryUtils; import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.jpa.DefaultJpaDialect; import org.springframework.orm.jpa.DefaultJpaDialect;
import org.springframework.orm.jpa.EntityManagerFactoryUtils; import org.springframework.orm.jpa.EntityManagerFactoryUtils;
@ -88,8 +88,7 @@ public class HibernateJpaDialect extends DefaultJpaDialect {
throws PersistenceException, SQLException { throws PersistenceException, SQLException {
Session session = getSession(entityManager); Session session = getSession(entityManager);
Connection con = session.connection(); return new HibernateConnectionHandle(session);
return (con != null ? new SimpleConnectionHandle(con) : null);
} }
@Override @Override
@ -138,4 +137,22 @@ public class HibernateJpaDialect extends DefaultJpaDialect {
} }
} }
private static class HibernateConnectionHandle implements ConnectionHandle {
private final Session session;
public HibernateConnectionHandle(Session session) {
this.session = session;
}
public Connection getConnection() {
return this.session.connection();
}
public void releaseConnection(Connection con) {
JdbcUtils.closeConnection(con);
}
}
} }