Skip to content

Commit 6300c34

Browse files
Narosbeikov
authored andcommitted
HHH-14725 Adjust test to use byte array, update Javadoc
1 parent 3f93ec8 commit 6300c34

File tree

2 files changed

+19
-1
lines changed
  • hibernate-core/src/main/java/org/hibernate/engine/jdbc
  • 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/BlobProxy.java

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

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)