File tree Expand file tree Collapse 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 Expand file tree Collapse file tree 2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -117,6 +117,11 @@ public static Blob generateProxy(byte[] bytes) {
117
117
/**
118
118
* Generates a BlobImpl proxy using a given number of bytes from an InputStream.
119
119
*
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
+ *
120
125
* @param stream The input stream of bytes to be created as a Blob.
121
126
* @param length The number of bytes from stream to be written to the Blob.
122
127
*
Original file line number Diff line number Diff line change @@ -47,7 +47,20 @@ public void initData() {
47
47
final InputStream stream = new BufferedInputStream ( Files .newInputStream ( path ) );
48
48
assertThat ( stream .markSupported (), Matchers .is ( true ) );
49
49
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 () );
51
64
52
65
asset .setData ( blob );
53
66
entityManager .persist ( asset );
You can’t perform that action at this time.
0 commit comments