From b4603e0e400fe33694204dc96847ef7fbe79aba3 Mon Sep 17 00:00:00 2001 From: Christian Beikov Date: Tue, 9 Apr 2024 10:57:56 +0200 Subject: [PATCH] HHH-17932 Get rid of unnecessary synchronization --- .../java/org/hibernate/dialect/Dialect.java | 18 +++++-- .../internal/util/io/StreamCopier.java | 50 ------------------- .../sql/ast/spi/SqlAliasBaseImpl.java | 8 ++- 3 files changed, 17 insertions(+), 59 deletions(-) delete mode 100644 hibernate-core/src/main/java/org/hibernate/internal/util/io/StreamCopier.java diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java index 2a83ca6111..9f7db4e42e 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/Dialect.java @@ -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" ); } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/io/StreamCopier.java b/hibernate-core/src/main/java/org/hibernate/internal/util/io/StreamCopier.java deleted file mode 100644 index 0453e42842..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/io/StreamCopier.java +++ /dev/null @@ -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 . - */ -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 ); - } - } -} diff --git a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAliasBaseImpl.java b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAliasBaseImpl.java index 98afec5658..b3b72a8457 100644 --- a/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAliasBaseImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/sql/ast/spi/SqlAliasBaseImpl.java @@ -28,11 +28,9 @@ 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; - } + final String alias = stem + "_" + ( aliasCount++ ); + SqlTreeCreationLogger.LOGGER.debugf( "Created new SQL alias : %s", alias ); + return alias; } @Override