Skip to content

Commit ed18b53

Browse files
ARTEMIS-5829 cache reflection methods on PostgresLargeObjectManager
notice there are no semantic changes here. Reflection is probably only used as part of our testsuite because of casspath issues.
1 parent 3e0cdf9 commit ed18b53

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

artemis-jdbc-store/src/main/java/org/apache/activemq/artemis/jdbc/store/file/PostgresLargeObjectManager.java

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ public PostgresLargeObjectManager() {
5757
}
5858
}
5959

60+
volatile Method createLOMethod;
6061
public final Long createLO(Connection connection) throws SQLException {
6162
if (shouldUseReflection) {
6263
Object largeObjectManager = getLargeObjectManager(connection);
6364
try {
64-
Method method = largeObjectManager.getClass().getMethod("createLO");
65-
return (Long) method.invoke(largeObjectManager);
65+
if (createLOMethod == null) {
66+
createLOMethod = largeObjectManager.getClass().getMethod("createLO");
67+
}
68+
return (Long) createLOMethod.invoke(largeObjectManager);
6669
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
6770
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObjectManager", ex);
6871
}
@@ -71,12 +74,15 @@ public final Long createLO(Connection connection) throws SQLException {
7174
}
7275
}
7376

77+
volatile Method deleteLOMethod;
7478
public final void deleteLO(Connection connection, long oid) throws SQLException {
7579
Object largeObjectManager = getLargeObjectManager(connection);
7680
if (shouldUseReflection) {
7781
try {
78-
Method method = largeObjectManager.getClass().getMethod("delete", long.class);
79-
method.invoke(largeObjectManager, oid);
82+
if (deleteLOMethod == null) {
83+
deleteLOMethod = largeObjectManager.getClass().getMethod("delete", long.class);
84+
}
85+
deleteLOMethod.invoke(largeObjectManager, oid);
8086
} catch (Exception ex) {
8187
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObjectManager", ex);
8288
}
@@ -88,12 +94,15 @@ public final void deleteLO(Connection connection, long oid) throws SQLException
8894

8995
}
9096

97+
volatile Method openMethod;
9198
public Object open(Connection connection, long oid, int mode) throws SQLException {
9299
if (shouldUseReflection) {
93100
Object largeObjectManager = getLargeObjectManager(connection);
94101
try {
95-
Method method = largeObjectManager.getClass().getMethod("open", long.class, int.class);
96-
return method.invoke(largeObjectManager, oid, mode);
102+
if (openMethod == null) {
103+
openMethod = largeObjectManager.getClass().getMethod("open", long.class, int.class);
104+
}
105+
return openMethod.invoke(largeObjectManager, oid, mode);
97106
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
98107
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObjectManager", ex);
99108
}
@@ -102,11 +111,14 @@ public Object open(Connection connection, long oid, int mode) throws SQLExceptio
102111
}
103112
}
104113

114+
volatile Method sizeMethod;
105115
public int size(Object largeObject) throws SQLException {
106116
if (shouldUseReflection) {
107117
try {
108-
Method method = largeObject.getClass().getMethod("size");
109-
return (int) method.invoke(largeObject);
118+
if (sizeMethod == null) {
119+
sizeMethod = largeObject.getClass().getMethod("size");
120+
}
121+
return (int) sizeMethod.invoke(largeObject);
110122
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
111123
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex);
112124
}
@@ -115,11 +127,14 @@ public int size(Object largeObject) throws SQLException {
115127
}
116128
}
117129

130+
volatile Method closeMethod;
118131
public void close(Object largeObject) throws SQLException {
119132
if (shouldUseReflection) {
120133
try {
121-
Method method = largeObject.getClass().getMethod("close");
122-
method.invoke(largeObject);
134+
if (closeMethod == null) {
135+
closeMethod = largeObject.getClass().getMethod("close");
136+
}
137+
closeMethod.invoke(largeObject);
123138
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
124139
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex);
125140
}
@@ -128,11 +143,14 @@ public void close(Object largeObject) throws SQLException {
128143
}
129144
}
130145

146+
volatile Method readMethod;
131147
public byte[] read(Object largeObject, int length) throws SQLException {
132148
if (shouldUseReflection) {
133149
try {
134-
Method method = largeObject.getClass().getMethod("read", int.class);
135-
return (byte[]) method.invoke(largeObject, length);
150+
if (readMethod == null) {
151+
readMethod = largeObject.getClass().getMethod("read", int.class);
152+
}
153+
return (byte[]) readMethod.invoke(largeObject, length);
136154
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
137155
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex);
138156
}
@@ -141,11 +159,14 @@ public byte[] read(Object largeObject, int length) throws SQLException {
141159
}
142160
}
143161

162+
volatile Method writeMethod;
144163
public void write(Object largeObject, byte[] data) throws SQLException {
145164
if (shouldUseReflection) {
146165
try {
147-
Method method = largeObject.getClass().getMethod("write", byte[].class);
148-
method.invoke(largeObject, data);
166+
if (writeMethod == null) {
167+
writeMethod = largeObject.getClass().getMethod("write", byte[].class);
168+
}
169+
writeMethod.invoke(largeObject, data);
149170
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
150171
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex);
151172
}
@@ -154,11 +175,14 @@ public void write(Object largeObject, byte[] data) throws SQLException {
154175
}
155176
}
156177

178+
volatile Method seekMethod;
157179
public void seek(Object largeObject, int position) throws SQLException {
158180
if (shouldUseReflection) {
159181
try {
160-
Method method = largeObject.getClass().getMethod("seek", int.class);
161-
method.invoke(largeObject, position);
182+
if (seekMethod == null) {
183+
seekMethod = largeObject.getClass().getMethod("seek", int.class);
184+
}
185+
seekMethod.invoke(largeObject, position);
162186
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
163187
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex);
164188
}
@@ -167,11 +191,14 @@ public void seek(Object largeObject, int position) throws SQLException {
167191
}
168192
}
169193

194+
volatile Method truncateMethod;
170195
public void truncate(Object largeObject, int position) throws SQLException {
171196
if (shouldUseReflection) {
172197
try {
173-
Method method = largeObject.getClass().getMethod("truncate", int.class);
174-
method.invoke(largeObject, position);
198+
if (truncateMethod == null) {
199+
truncateMethod = largeObject.getClass().getMethod("truncate", int.class);
200+
}
201+
truncateMethod.invoke(largeObject, position);
175202
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
176203
throw new SQLException("Couldn't access org.postgresql.largeobject.LargeObject", ex);
177204
}

0 commit comments

Comments
 (0)