@@ -48,18 +48,15 @@ public final class ByteBufProxy extends BufferProxy<ByteBuf> {
4848 private static final ThreadLocal <ArrayDeque <ByteBuf >> BUFFERS = withInitial (()
4949 -> new ArrayDeque <>(16 ));
5050
51+ private static final int BUFFER_RETRIES = 10 ;
5152 private static final String FIELD_NAME_ADDRESS = "memoryAddress" ;
5253 private static final String FIELD_NAME_LENGTH = "length" ;
5354 private static final long LENGTH_OFFSET ;
5455 private static final String NAME = "io.netty.buffer.PooledUnsafeDirectByteBuf" ;
5556
5657 static {
5758 try {
58- // create buffer (first is SimpleLeakAwareByteBuff but we need PooledUDBB)
59- final ByteBuf bb = DEFAULT .directBuffer (0 );
60- if (!NAME .equals (bb .getClass ().getName ())) {
61- throw new IllegalStateException ("Netty buffer must be " + NAME );
62- }
59+ createBuffer ();
6360 final Field address = findField (NAME , FIELD_NAME_ADDRESS );
6461 final Field length = findField (NAME , FIELD_NAME_LENGTH );
6562 ADDRESS_OFFSET = UNSAFE .objectFieldOffset (address );
@@ -88,6 +85,16 @@ static Field findField(final String c, final String name) {
8885 throw new LmdbException (name + " not found" );
8986 }
9087
88+ private static ByteBuf createBuffer () {
89+ for (int i = 0 ; i < BUFFER_RETRIES ; i ++) {
90+ final ByteBuf bb = DEFAULT .directBuffer (0 );
91+ if (NAME .equals (bb .getClass ().getName ())) {
92+ return bb ;
93+ }
94+ }
95+ throw new IllegalStateException ("Netty buffer must be " + NAME );
96+ }
97+
9198 @ Override
9299 protected ByteBuf allocate () {
93100 final ArrayDeque <ByteBuf > queue = BUFFERS .get ();
@@ -96,7 +103,7 @@ protected ByteBuf allocate() {
96103 if (buffer != null && buffer .capacity () >= 0 ) {
97104 return buffer ;
98105 } else {
99- return DEFAULT . directBuffer ( 0 );
106+ return createBuffer ( );
100107 }
101108 }
102109
0 commit comments