HHH-5550 - Hibernate.createBlob() fails when used in current_session_context_class=thread mode

This commit is contained in:
Steve Ebersole 2011-01-21 11:07:59 -06:00 committed by JPAV
parent 7c3aafb7c2
commit 144dde0080
2 changed files with 78 additions and 7 deletions

View File

@ -72,11 +72,12 @@ public class ThreadLocalSessionContext implements CurrentSessionContext {
private static final HibernateLogger LOG = Logger.getMessageLogger(HibernateLogger.class, private static final HibernateLogger LOG = Logger.getMessageLogger(HibernateLogger.class,
ThreadLocalSessionContext.class.getName()); ThreadLocalSessionContext.class.getName());
private static final Class[] SESS_PROXY_INTERFACES = new Class[] { private static final Class[] SESSION_PROXY_INTERFACES = new Class[] {
org.hibernate.classic.Session.class, org.hibernate.classic.Session.class,
org.hibernate.engine.SessionImplementor.class, org.hibernate.engine.SessionImplementor.class,
org.hibernate.engine.jdbc.spi.JDBCContext.Context.class, org.hibernate.engine.jdbc.spi.JDBCContext.Context.class,
org.hibernate.event.EventSource.class org.hibernate.event.EventSource.class,
org.hibernate.engine.jdbc.LobCreationContext.class
}; };
/** /**
@ -85,7 +86,7 @@ public class ThreadLocalSessionContext implements CurrentSessionContext {
* the possibility for multiple SessionFactorys being used during execution * the possibility for multiple SessionFactorys being used during execution
* of the given thread. * of the given thread.
*/ */
private static final ThreadLocal context = new ThreadLocal(); private static final ThreadLocal<Map> context = new ThreadLocal<Map>();
protected final SessionFactoryImplementor factory; protected final SessionFactoryImplementor factory;
@ -100,7 +101,7 @@ public class ThreadLocalSessionContext implements CurrentSessionContext {
Session current = existingSession( factory ); Session current = existingSession( factory );
if (current == null) { if (current == null) {
current = buildOrObtainSession(); current = buildOrObtainSession();
// register a cleanup synch // register a cleanup sync
current.getTransaction().registerSynchronization( buildCleanupSynch() ); current.getTransaction().registerSynchronization( buildCleanupSynch() );
// wrap the session in the transaction-protection proxy // wrap the session in the transaction-protection proxy
if ( needsWrapping( current ) ) { if ( needsWrapping( current ) ) {
@ -181,7 +182,7 @@ public class ThreadLocalSessionContext implements CurrentSessionContext {
TransactionProtectionWrapper wrapper = new TransactionProtectionWrapper( session ); TransactionProtectionWrapper wrapper = new TransactionProtectionWrapper( session );
Session wrapped = ( Session ) Proxy.newProxyInstance( Session wrapped = ( Session ) Proxy.newProxyInstance(
Session.class.getClassLoader(), Session.class.getClassLoader(),
SESS_PROXY_INTERFACES, SESSION_PROXY_INTERFACES,
wrapper wrapper
); );
// yick! need this for proper serialization/deserialization handling... // yick! need this for proper serialization/deserialization handling...
@ -222,8 +223,9 @@ public class ThreadLocalSessionContext implements CurrentSessionContext {
} }
/** /**
* Unassociate a previously bound session from the current thread of execution. * Disassociates a previously bound session from the current thread of execution.
* *
* @param factory The factory for which the session should be unbound.
* @return The session which was unbound. * @return The session which was unbound.
*/ */
public static Session unbind(SessionFactory factory) { public static Session unbind(SessionFactory factory) {
@ -237,9 +239,10 @@ public class ThreadLocalSessionContext implements CurrentSessionContext {
} }
protected static Map sessionMap() { protected static Map sessionMap() {
return ( Map ) context.get(); return context.get();
} }
@SuppressWarnings({"unchecked"})
private static void doBind(org.hibernate.Session session, SessionFactory factory) { private static void doBind(org.hibernate.Session session, SessionFactory factory) {
Map sessionMap = sessionMap(); Map sessionMap = sessionMap();
if ( sessionMap == null ) { if ( sessionMap == null ) {

View File

@ -0,0 +1,68 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.connections;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.testing.junit.functional.FunctionalTestCase;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
/**
* Test originally developed to verify and fix HHH-5550
*
* @author Steve Ebersole
*/
public class HibernateCreateBlobFailedCase extends FunctionalTestCase {
public HibernateCreateBlobFailedCase(String string) {
super( string );
}
@Override
public String[] getMappings() {
return new String[] { "connections/Silly.hbm.xml" };
}
@Override
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread" );
}
public void testLobCreation() throws SQLException {
Session session = sfi().getCurrentSession();
session.beginTransaction();
Blob blob = Hibernate.getLobCreator( session ).createBlob( new byte[] {} );
blob.free();
Clob clob = Hibernate.getLobCreator( session ).createClob( "Steve" );
clob.free();
session.getTransaction().commit();
assertFalse( session.isOpen() );
}
}