Skip to content

Commit 922997c

Browse files
Tomasz-Marciniakmp911de
authored andcommitted
Release ByteBuf after encoding.
Signed-off-by: Tomasz Marciniak <[email protected]> [resolves #279][#298]
1 parent 2ed8648 commit 922997c

7 files changed

Lines changed: 32 additions & 7 deletions

File tree

src/jmh/java/io/r2dbc/mssql/PooledBenchmarks.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ public ConnectionHolder() {
6161

6262
this.jdbc = extension.getDataSource();
6363

64-
MssqlConnectionConfiguration configuration =
65-
MssqlConnectionConfiguration.builder().host(extension.getHost()).username(extension.getUsername()).password(extension.getPassword()).build();
64+
MssqlConnectionConfiguration configuration = extension.configBuilder().build();
6665

6766
MssqlConnectionFactory mssqlConnectionFactory = new MssqlConnectionFactory(configuration);
6867
ConnectionPoolConfiguration poolConfiguration = ConnectionPoolConfiguration.builder(mssqlConnectionFactory).maxSize(4).build();
@@ -90,6 +89,7 @@ public void simpleDirectJdbc(ConnectionHolder connectionHolder, Blackhole voodoo
9089
}
9190

9291
@Benchmark
92+
@Testable
9393
public void simpleDirectR2dbc(ConnectionHolder connectionHolder, Blackhole voodoo) {
9494

9595
String optname = Mono.usingWhen(connectionHolder.r2dbc.create(), connection -> {

src/main/java/io/r2dbc/mssql/codec/BlobCodec.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,14 @@ public Publisher<ByteBuffer> stream() {
166166
result.flip();
167167
return result;
168168
})
169-
.doOnDiscard(ByteBuf.class, ByteBuf::release)
169+
.doOnDiscard(ByteBuf.class, buffer ->
170+
{
171+
if (buffer.refCnt() > 0) {
172+
buffer.release();
173+
}
174+
}
175+
)
170176
.doOnCancel(() -> {
171-
172177
for (ByteBuf buffer : this.buffers) {
173178
if (buffer.refCnt() > 0) {
174179
buffer.release();

src/main/java/io/r2dbc/mssql/codec/ByteArray.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ static byte[] fromEncoded(Function<ByteBufAllocator, Encoded> encodeFunction) {
4242

4343
Encoded encoded = encodeFunction.apply(ByteBufAllocator.DEFAULT);
4444

45+
ByteBuf buffer = null;
4546
try {
46-
return ByteBufUtil.getBytes(encoded.getValue());
47+
buffer = encoded.getValue();
48+
return ByteBufUtil.getBytes(buffer);
4749
} finally {
48-
encoded.dispose();
50+
if (buffer != null && buffer.refCnt() > 0) {
51+
buffer.release();
52+
}
4953
}
5054
}
5155

src/main/java/io/r2dbc/mssql/message/token/Attention.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public TdsFragment encode(ByteBufAllocator allocator, int packetSize) {
7979
ByteBuf buffer = allocator.buffer(length);
8080
encode(buffer);
8181

82+
if(buffer.refCnt() > 0) {
83+
buffer.release();
84+
}
85+
8286
return TdsPackets.create(header, Unpooled.EMPTY_BUFFER);
8387
}
8488

src/test/java/io/r2dbc/mssql/message/token/RowTokenUnitTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ void shouldDecodeRow() {
6767
assertThat(rowToken.getColumnData(1)).isNotNull();
6868
assertThat(rowToken.getColumnData(2)).isNotNull();
6969
assertThat(rowToken.getColumnData(3)).isNotNull();
70+
buffer.release();
7071
}
7172

7273
@Test
@@ -111,6 +112,9 @@ void shouldDecodeIntAndVarcharMax() throws IOException {
111112

112113
assertThat(row.getColumnData(0).readableBytes()).isEqualTo(5);
113114
assertThat(row.getColumnData(1).readableBytes()).isEqualTo(10016);
115+
116+
rowData.release();
117+
row.release();
114118
}
115119

116120
@Test
@@ -129,6 +133,9 @@ void shouldDecodeIntAndVarcharMaxNull() {
129133

130134
assertThat(row.getColumnData(0).readableBytes()).isEqualTo(5);
131135
assertThat(row.getColumnData(1)).isNull();
136+
137+
rowData.release();
138+
row.release();
132139
}
133140

134141
@Test
@@ -143,6 +150,7 @@ void canDecodeShouldReportPlpDecodability() throws IOException {
143150

144151
ByteBuf rowData = loadRowData("int-varcharmax-data.txt");
145152
CanDecodeTestSupport.testCanDecode(rowData, buffer -> RowToken.canDecode(buffer, columns.getColumns()));
153+
rowData.release();
146154
}
147155

148156
@Test

src/test/java/io/r2dbc/mssql/util/EncodedAssert.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ public EncodedAssert isEncodedAs(Consumer<ByteBuf> encoded) {
119119
Assertions.assertThat(ByteBufUtil.prettyHexDump(this.actual)).describedAs("ByteBuf")
120120
.isEqualTo(ByteBufUtil.prettyHexDump(expected));
121121

122+
if(this.actual.refCnt()>0){
123+
this.actual.release();
124+
}
125+
122126
return this;
123127
}
124128
}

src/test/java/io/r2dbc/mssql/util/MsSqlServerExtension.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void afterAll(ExtensionContext context) {
9090
}
9191

9292
public MssqlConnectionConfiguration.Builder configBuilder() {
93-
return MssqlConnectionConfiguration.builder().host(getHost()).username(getUsername()).password(getPassword());
93+
return MssqlConnectionConfiguration.builder().host(getHost()).port(getPort()).username(getUsername()).password(getPassword());
9494
}
9595

9696
public MssqlConnectionConfiguration getConnectionConfiguration() {

0 commit comments

Comments
 (0)