Skip to content

Commit 74e4c21

Browse files
committed
Dont close transaction in MCC
1 parent 27032df commit 74e4c21

1 file changed

Lines changed: 56 additions & 60 deletions

File tree

mcc/src/org/labkey/mcc/query/TriggerHelper.java

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -299,83 +299,79 @@ private TableInfo getMappingTable()
299299

300300
public int ensureMccAliasExists(Collection<String> rawIds, Map<Object, Object> existingAliases)
301301
{
302-
try (DbScope.Transaction transaction = StudyService.get().getDatasetSchema().getScope().ensureTransaction())
302+
if (!DbScope.getLabKeyScope().isTransactionActive())
303303
{
304-
if (transaction.isAborted())
305-
{
306-
return 0;
307-
}
304+
_log.info("No active transaction, skipping MCC ensureMccAliasExists()");
305+
return 0;
306+
}
308307

309-
// NOTE: The incoming object can convert numeric IDs from strings to int, so manually convert:
310-
// Also, CaseInsensitiveSet will convert the keys to lowercase, which is problematic for case-sensitive databases
311-
final CaseInsensitiveHashMap<String> idMap = new CaseInsensitiveHashMap<>();
312-
rawIds.stream().map(String::valueOf).forEach(x -> idMap.put(x, x));
308+
// NOTE: The incoming object can convert numeric IDs from strings to int, so manually convert:
309+
// Also, CaseInsensitiveSet will convert the keys to lowercase, which is problematic for case-sensitive databases
310+
final CaseInsensitiveHashMap<String> idMap = new CaseInsensitiveHashMap<>();
311+
rawIds.stream().map(String::valueOf).forEach(x -> idMap.put(x, x));
313312

314-
CaseInsensitiveHashMap<String> ciExistingAliases = new CaseInsensitiveHashMap<>();
315-
existingAliases.forEach((key, val) -> ciExistingAliases.put(String.valueOf(key), String.valueOf(val)));
313+
CaseInsensitiveHashMap<String> ciExistingAliases = new CaseInsensitiveHashMap<>();
314+
existingAliases.forEach((key, val) -> ciExistingAliases.put(String.valueOf(key), String.valueOf(val)));
316315

317-
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("subjectname"), idMap.values(), CompareType.IN);
316+
SimpleFilter filter = new SimpleFilter(FieldKey.fromString("subjectname"), idMap.values(), CompareType.IN);
318317

319-
final Set<String> aliasesFound = new HashSet<>();
320-
TableInfo ti = getMappingTable();
321-
new TableSelector(ti, PageFlowUtil.set("subjectname", "externalAlias"), filter, null).forEachResults(rs -> {
322-
aliasesFound.add(rs.getString(FieldKey.fromString("subjectname")));
323-
if (ciExistingAliases.containsKey(rs.getString(FieldKey.fromString("subjectname"))))
318+
final Set<String> aliasesFound = new HashSet<>();
319+
TableInfo ti = getMappingTable();
320+
new TableSelector(ti, PageFlowUtil.set("subjectname", "externalAlias"), filter, null).forEachResults(rs -> {
321+
aliasesFound.add(rs.getString(FieldKey.fromString("subjectname")));
322+
if (ciExistingAliases.containsKey(rs.getString(FieldKey.fromString("subjectname"))))
323+
{
324+
if (!ciExistingAliases.get(rs.getString(FieldKey.fromString("subjectname"))).equalsIgnoreCase(rs.getString(FieldKey.fromString("externalAlias"))))
324325
{
325-
if (!ciExistingAliases.get(rs.getString(FieldKey.fromString("subjectname"))).equalsIgnoreCase(rs.getString(FieldKey.fromString("externalAlias"))))
326-
{
327-
_log.error("Incoming MCC alias for: " + rs.getString(FieldKey.fromString("subjectname")) + "(" + ciExistingAliases.get(rs.getString(FieldKey.fromString("subjectname"))) + ") does not match existing: " + rs.getString(FieldKey.fromString("externalAlias")));
328-
}
326+
_log.error("Incoming MCC alias for: " + rs.getString(FieldKey.fromString("subjectname")) + "(" + ciExistingAliases.get(rs.getString(FieldKey.fromString("subjectname"))) + ") does not match existing: " + rs.getString(FieldKey.fromString("externalAlias")));
329327
}
330-
});
331-
332-
aliasesFound.forEach(idMap::remove);
333-
if (idMap.isEmpty())
334-
{
335-
return 0;
336328
}
329+
});
337330

338-
final List<Map<String, Object>> toAdd = new ArrayList<>();
339-
try
340-
{
341-
AtomicInteger aliasesReused = new AtomicInteger(0);
342-
idMap.forEach((key, id) -> {
343-
CaseInsensitiveHashMap<Object> row = new CaseInsensitiveHashMap<>();
344-
row.put("subjectname", id);
345-
if (ciExistingAliases.containsKey(id))
346-
{
347-
_log.info("Will re-use existing MCC alias: " + ciExistingAliases.get(id) + ", for ID: " + id);
348-
aliasesReused.getAndIncrement();
349-
}
350-
351-
row.put("externalAlias", ciExistingAliases.get(id)); //NOTE: the trigger script will auto-assign a value if null, but we need to include this property on the input JSON
352-
353-
toAdd.add(row);
354-
});
355-
356-
if (!ciExistingAliases.isEmpty() && aliasesReused.get() != ciExistingAliases.size())
357-
{
358-
_log.info("The existing aliases map, size: " + ciExistingAliases.size() + " does not equal the number of aliases actually used, which was: " + aliasesReused.get());
359-
_log.info(ciExistingAliases);
360-
}
331+
aliasesFound.forEach(idMap::remove);
332+
if (idMap.isEmpty())
333+
{
334+
return 0;
335+
}
361336

362-
BatchValidationException bve = new BatchValidationException();
363-
ti.getUpdateService().insertRows(_user, _container, toAdd, bve, null, null);
364-
if (bve.hasErrors())
337+
final List<Map<String, Object>> toAdd = new ArrayList<>();
338+
try
339+
{
340+
AtomicInteger aliasesReused = new AtomicInteger(0);
341+
idMap.forEach((key, id) -> {
342+
CaseInsensitiveHashMap<Object> row = new CaseInsensitiveHashMap<>();
343+
row.put("subjectname", id);
344+
if (ciExistingAliases.containsKey(id))
365345
{
366-
throw bve;
346+
_log.info("Will re-use existing MCC alias: " + ciExistingAliases.get(id) + ", for ID: " + id);
347+
aliasesReused.getAndIncrement();
367348
}
368349

369-
transaction.commit();
350+
row.put("externalAlias", ciExistingAliases.get(id)); //NOTE: the trigger script will auto-assign a value if null, but we need to include this property on the input JSON
351+
352+
toAdd.add(row);
353+
});
370354

371-
return toAdd.size();
355+
if (!ciExistingAliases.isEmpty() && aliasesReused.get() != ciExistingAliases.size())
356+
{
357+
_log.info("The existing aliases map, size: " + ciExistingAliases.size() + " does not equal the number of aliases actually used, which was: " + aliasesReused.get());
358+
_log.info(ciExistingAliases);
372359
}
373-
catch (BatchValidationException | DuplicateKeyException | QueryUpdateServiceException | SQLException e)
360+
361+
BatchValidationException bve = new BatchValidationException();
362+
ti.getUpdateService().insertRows(_user, _container, toAdd, bve, null, null);
363+
if (bve.hasErrors())
374364
{
375-
_log.error("Error auto-creating MCC aliases during insert for folder: " + _container.getPath(), e);
376-
toAdd.forEach(_log::error);
377-
return 0;
365+
throw bve;
378366
}
367+
368+
return toAdd.size();
369+
}
370+
catch (BatchValidationException | DuplicateKeyException | QueryUpdateServiceException | SQLException e)
371+
{
372+
_log.error("Error auto-creating MCC aliases during insert for folder: " + _container.getPath(), e);
373+
toAdd.forEach(_log::error);
374+
return 0;
379375
}
380376
}
381377

0 commit comments

Comments
 (0)