HHH-17932 Get rid of unnecessary synchronization

This commit is contained in:
Christian Beikov 2024-04-09 10:57:56 +02:00
parent 95403bc083
commit b4603e0e40
3 changed files with 17 additions and 59 deletions

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.dialect;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
@ -40,6 +41,7 @@ import java.util.TimeZone;
import java.util.UUID;
import java.util.regex.Pattern;
import org.hibernate.HibernateException;
import org.hibernate.Incubating;
import org.hibernate.Length;
import org.hibernate.LockMode;
@ -107,7 +109,6 @@ import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.MathHelper;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.internal.util.io.StreamCopier;
import org.hibernate.loader.ast.spi.MultiKeyLoadSizingStrategy;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Constraint;
@ -1755,9 +1756,12 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun
final OutputStream connectedStream = target.setBinaryStream( 1L );
// the BLOB from the detached state
final InputStream detachedStream = original.getBinaryStream();
StreamCopier.copy( detachedStream, connectedStream );
detachedStream.transferTo( connectedStream );
return target;
}
catch (IOException e ) {
throw new HibernateException( "Unable to copy stream content", e );
}
catch (SQLException e ) {
throw session.getFactory().getJdbcServices().getSqlExceptionHelper()
.convert( e, "unable to merge BLOB data" );
@ -1776,9 +1780,12 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun
final OutputStream connectedStream = target.setAsciiStream( 1L );
// the CLOB from the detached state
final InputStream detachedStream = original.getAsciiStream();
StreamCopier.copy( detachedStream, connectedStream );
detachedStream.transferTo( connectedStream );
return target;
}
catch (IOException e ) {
throw new HibernateException( "Unable to copy stream content", e );
}
catch (SQLException e ) {
throw session.getFactory().getJdbcServices().getSqlExceptionHelper().convert( e, "unable to merge CLOB data" );
}
@ -1796,9 +1803,12 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun
final OutputStream connectedStream = target.setAsciiStream( 1L );
// the NCLOB from the detached state
final InputStream detachedStream = original.getAsciiStream();
StreamCopier.copy( detachedStream, connectedStream );
detachedStream.transferTo( connectedStream );
return target;
}
catch (IOException e ) {
throw new HibernateException( "Unable to copy stream content", e );
}
catch (SQLException e ) {
throw session.getFactory().getJdbcServices().getSqlExceptionHelper().convert( e, "unable to merge NCLOB data" );
}

View File

@ -1,50 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.internal.util.io;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.hibernate.HibernateException;
/**
* Utilities for copying I/O streams.
*
* @author Steve Ebersole
*/
public final class StreamCopier {
private StreamCopier() {
}
public static final int BUFFER_SIZE = 1024 * 4;
public static final byte[] BUFFER = new byte[ BUFFER_SIZE ];
public static long copy(InputStream from, OutputStream into) {
try {
long totalRead = 0;
while ( true ) {
synchronized ( BUFFER ) {
int amountRead = from.read( BUFFER );
if ( amountRead == -1 ) {
break;
}
into.write( BUFFER, 0, amountRead );
totalRead += amountRead;
if ( amountRead < BUFFER_SIZE ) {
// should mean there is no more data in the stream, no need for next read
break;
}
}
}
return totalRead;
}
catch (IOException e ) {
throw new HibernateException( "Unable to copy stream content", e );
}
}
}

View File

@ -28,12 +28,10 @@ public class SqlAliasBaseImpl implements SqlAliasBase {
@Override
public String generateNewAlias() {
synchronized (this) {
final String alias = stem + "_" + ( aliasCount++ );
SqlTreeCreationLogger.LOGGER.debugf( "Created new SQL alias : %s", alias );
return alias;
}
}
@Override
public String toString() {