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:10 -06:00
parent bbc338c053
commit a1266b9103
3 changed files with 78 additions and 5 deletions

View File

@ -453,7 +453,8 @@ public final class Hibernate {
* @param bytes a byte array * @param bytes a byte array
* @param session The session in which the {@link Blob} will be used. * @param session The session in which the {@link Blob} will be used.
* @return the Blob * @return the Blob
* @deprecated Use {@link LobHelper#createBlob(byte[])} instead. * @deprecated Use {@link LobHelper#createBlob(byte[])} instead on the {@link LobHelper} obtained from
* {@link Session#getLobHelper()}
*/ */
public static Blob createBlob(byte[] bytes, Session session) { public static Blob createBlob(byte[] bytes, Session session) {
return getLobCreator( session ).createBlob( bytes ); return getLobCreator( session ).createBlob( bytes );
@ -553,7 +554,8 @@ public final class Hibernate {
* @param string The string data * @param string The string data
* @param session The session in which the {@link Clob} will be used. * @param session The session in which the {@link Clob} will be used.
* @return The created {@link Clob} * @return The created {@link Clob}
* @deprecated Use {@link LobHelper#createClob(String)} instead * @deprecated Use {@link LobHelper#createClob(String)} instead on the {@link LobHelper} obtained from
* {@link Session#getLobHelper()}
*/ */
@Deprecated @Deprecated
public static Clob createClob(String string, Session session) { public static Clob createClob(String string, Session session) {

View File

@ -73,11 +73,12 @@ import org.hibernate.engine.SessionFactoryImplementor;
public class ThreadLocalSessionContext implements CurrentSessionContext { public class ThreadLocalSessionContext implements CurrentSessionContext {
private static final Logger log = LoggerFactory.getLogger( ThreadLocalSessionContext.class ); private static final Logger log = LoggerFactory.getLogger( ThreadLocalSessionContext.class );
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.jdbc.JDBCContext.Context.class, org.hibernate.jdbc.JDBCContext.Context.class,
org.hibernate.event.EventSource.class org.hibernate.event.EventSource.class,
org.hibernate.engine.jdbc.LobCreationContext.class
}; };
/** /**
@ -182,7 +183,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...

View File

@ -0,0 +1,70 @@
/*
* 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 );
}
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();
blob = Hibernate.createBlob( new byte[] {}, session );
blob.free();
Clob clob = Hibernate.getLobCreator( session ).createClob( "Steve" );
clob.free();
clob = Hibernate.createClob( "Steve", session );
clob.free();
session.getTransaction().commit();
assertFalse( session.isOpen() );
}
}