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 @@ -111,6 +111,11 @@ public static Blob generateProxy(byte[] bytes) {
111111 /**
112112 * Generates a BlobImpl proxy using a given number of bytes from an InputStream.
113113 *
114+ * Be aware that certain database drivers will automatically close the provided InputStream after the
115+ * contents have been written to the database. This may cause unintended side effects if the entity
116+ * is also audited by Envers. In this case, it's recommended to use {@link #generateProxy(byte[])}
117+ * instead as it isn't affected by this non-standard behavior.
118+ *
114119 * @param stream The input stream of bytes to be created as a Blob.
115120 * @param length The number of bytes from stream to be written to the Blob.
116121 *
Original file line number Diff line number Diff line change @@ -49,7 +49,20 @@ public void initData() {
4949 final InputStream stream = new BufferedInputStream ( Files .newInputStream ( path ) );
5050 assertThat ( stream .markSupported (), Matchers .is ( true ) );
5151
52- Blob blob = BlobProxy .generateProxy ( stream , Files .size ( path ) );
52+ // We use the method readAllBytes instead of passing the raw stream to the proxy
53+ // since this is the only guaranteed way that will work across all dialects in a
54+ // deterministic way. Postgres and Sybase will automatically close the stream
55+ // after the blob has been written by the driver, which prevents Envers from
56+ // then writing the contents of the stream to the audit table.
57+ //
58+ // If the driver and dialect are known not to close the input stream after the
59+ // contents have been written by the driver, then it's safe to pass the stream
60+ // here instead and the stream will be automatically marked and reset so that
61+ // Envers can serialize the data after Hibernate has done so. Dialects like
62+ // H2, MySQL, Oracle, SQL Server work this way.
63+ //
64+ //
65+ Blob blob = BlobProxy .generateProxy ( stream .readAllBytes () );
5366
5467 asset .setData ( blob );
5568 entityManager .persist ( asset );
You can’t perform that action at this time.
0 commit comments