Skip to content

Commit a886aa1

Browse files
Narosdreab8
authored andcommitted
HHH-14725 Adjust test to use byte array, update Javadoc
1 parent 367d3f0 commit a886aa1

File tree

2 files changed

+19
-1
lines changed
  • hibernate-core/src/main/java/org/hibernate/engine/jdbc/proxy
  • hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/blob

2 files changed

+19
-1
lines changed

hibernate-core/src/main/java/org/hibernate/engine/jdbc/proxy/BlobProxy.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ public static Blob generateProxy(byte[] bytes) {
120120
/**
121121
* Generates a BlobImpl proxy using a given number of bytes from an InputStream.
122122
*
123+
* Be aware that certain database drivers will automatically close the provided InputStream after the
124+
* contents have been written to the database. This may cause unintended side effects if the entity
125+
* is also audited by Envers. In this case, it's recommended to use {@link #generateProxy(byte[])}
126+
* instead as it isn't affected by this non-standard behavior.
127+
*
123128
* @param stream The input stream of bytes to be created as a Blob.
124129
* @param length The number of bytes from stream to be written to the Blob.
125130
*

hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/blob/BasicBlobTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,20 @@ public void initData() {
4747
final InputStream stream = new BufferedInputStream( Files.newInputStream( path ) );
4848
assertThat( stream.markSupported(), Matchers.is( true ) );
4949

50-
Blob blob = BlobProxy.generateProxy( stream, Files.size( path ) );
50+
// We use the method readAllBytes instead of passing the raw stream to the proxy
51+
// since this is the only guaranteed way that will work across all dialects in a
52+
// deterministic way. Postgres and Sybase will automatically close the stream
53+
// after the blob has been written by the driver, which prevents Envers from
54+
// then writing the contents of the stream to the audit table.
55+
//
56+
// If the driver and dialect are known not to close the input stream after the
57+
// contents have been written by the driver, then it's safe to pass the stream
58+
// here instead and the stream will be automatically marked and reset so that
59+
// Envers can serialize the data after Hibernate has done so. Dialects like
60+
// H2, MySQL, Oracle, SQL Server work this way.
61+
//
62+
//
63+
Blob blob = BlobProxy.generateProxy( stream.readAllBytes() );
5164

5265
asset.setData( blob );
5366
entityManager.persist( asset );

0 commit comments

Comments
 (0)