@@ -93,11 +93,7 @@ public void writeEntity(
93
93
boolean nameOrParentChanged ,
94
94
PolarisBaseEntity originalEntity ) {
95
95
try {
96
- datasourceOperations .runWithinTransaction (
97
- statement -> {
98
- persistEntity (callCtx , entity , originalEntity , statement );
99
- return true ;
100
- });
96
+ persistEntity (callCtx , entity , originalEntity , datasourceOperations );
101
97
} catch (SQLException e ) {
102
98
throw new RuntimeException ("Error persisting entity" , e );
103
99
}
@@ -143,12 +139,12 @@ private void persistEntity(
143
139
@ Nonnull PolarisCallContext callCtx ,
144
140
@ Nonnull PolarisBaseEntity entity ,
145
141
PolarisBaseEntity originalEntity ,
146
- Statement statement )
142
+ Object executor )
147
143
throws SQLException {
148
144
ModelEntity modelEntity = ModelEntity .fromEntity (entity );
149
145
if (originalEntity == null ) {
150
146
try {
151
- statement . executeUpdate ( generateInsertQuery (modelEntity , realmId ));
147
+ execute ( executor , generateInsertQuery (modelEntity , realmId ));
152
148
} catch (SQLException e ) {
153
149
if (datasourceOperations .isConstraintViolation (e )) {
154
150
PolarisBaseEntity existingEntity =
@@ -176,7 +172,7 @@ private void persistEntity(
176
172
"realm_id" ,
177
173
realmId );
178
174
try {
179
- int rowsUpdated = statement . executeUpdate ( generateUpdateQuery (modelEntity , params ));
175
+ int rowsUpdated = execute ( executor , generateUpdateQuery (modelEntity , params ));
180
176
if (rowsUpdated == 0 ) {
181
177
throw new RetryOnConcurrencyException (
182
178
"Entity '%s' id '%s' concurrently modified; expected version %s" ,
@@ -189,6 +185,17 @@ private void persistEntity(
189
185
}
190
186
}
191
187
188
+ private int execute (Object executor , String query ) throws SQLException {
189
+ if (executor instanceof Statement ) {
190
+ // used for running in transaction
191
+ return ((Statement ) executor ).executeUpdate (query );
192
+ } else if (executor instanceof DatasourceOperations ) {
193
+ return ((DatasourceOperations ) executor ).executeUpdate (query );
194
+ } else {
195
+ throw new IllegalArgumentException ("Unsupported executor: " + executor );
196
+ }
197
+ }
198
+
192
199
@ Override
193
200
public void writeToGrantRecords (
194
201
@ Nonnull PolarisCallContext callCtx , @ Nonnull PolarisGrantRecord grantRec ) {
0 commit comments