public void close(ThreadLocal<DelegatingDbSessionSupplier> dbSessionThreadLocal, String label) { DelegatingDbSessionSupplier delegatingDbSessionSupplier = dbSessionThreadLocal.get(); boolean getCalled = delegatingDbSessionSupplier.isPopulated(); if (getCalled) { try { DbSession res = delegatingDbSessionSupplier.get(); res.close(); } catch (Exception e) { LOG.error(format("Failed to close %s connection in %s", label, currentThread()), e); } } }
@Override public void close() { try { iterator.close(); } finally { DatabaseUtils.closeQuietly(stmt); session.close(); } }
@Override public void start() { DbSession dbSession = dbClient.openSession(false); try { List<MetricDto> metricList = dbClient.metricDao().selectEnabled(dbSession); this.metricsByKey = from(metricList).transform(MetricDtoToMetric.INSTANCE).uniqueIndex(MetricToKey.INSTANCE); this.metricsById = from(metricList).transform(MetricDtoToMetric.INSTANCE).uniqueIndex(MetricToId.INSTANCE); } finally { dbSession.close(); } }
IssueIteratorForSingleChunk(DbClient dbClient, @Nullable String projectUuid, @Nullable Collection<String> issueKeys) { checkArgument(issueKeys == null || issueKeys.size() <= DatabaseUtils.PARTITION_SIZE_FOR_ORACLE, "Cannot search for more than " + DatabaseUtils.PARTITION_SIZE_FOR_ORACLE + " issue keys at once. Please provide the keys in smaller chunks."); this.projectUuid = projectUuid; this.issueKeys = issueKeys; this.session = dbClient.openSession(false); try { String sql = createSql(); stmt = dbClient.getMyBatis().newScrollingSelectStatement(session, sql); iterator = createIterator(); } catch (Exception e) { session.close(); throw new IllegalStateException("Fail to prepare SQL request to select all issues", e); } }
@After public void tearDown() { dbSession.close(); }
@After public void tearDown() { if (dbSession != null) { dbSession.close(); } }
@After public void tearDown() { session.close(); }
@After public void tearDown() { batchDbSession.close(); }
@After public void after() { dbSession.close(); }
@After public void tearDown() { session.close(); }
@Override protected void after() { if (session != null) { session.rollback(); session.close(); } db.stop(); started = false; }
@Test public void openSession_with_caching_returns_DbSession_that_does_not_roll_back_on_close_if_any_mutation_call_was_followed_by_rollback_with_parameters() throws SQLException { boolean force = random.nextBoolean(); DbSession dbSession = openSessionAndDoSeveralMutatingAndNeutralCalls(); dbSession.rollback(force); dbSession.close(); verify(myBatisDbSession, times(1)).rollback(force); verify(myBatisDbSession, times(0)).rollback(); }
@Test public void openSession_with_caching_returns_DbSession_that_does_not_roll_back_on_close_if_any_mutation_call_was_followed_by_commit() throws SQLException { DbSession dbSession = openSessionAndDoSeveralMutatingAndNeutralCalls(); COMMIT_CALLS[random.nextBoolean() ? 0 : 1].consume(dbSession); dbSession.close(); verify(myBatisDbSession, times(0)).rollback(); }
@Test public void openSession_with_caching_returns_DbSession_that_rolls_back_on_close_if_any_mutation_call_was_not_followed_by_commit_nor_rollback() throws SQLException { DbSession dbSession = openSessionAndDoSeveralMutatingAndNeutralCalls(); dbSession.close(); verify(myBatisDbSession).rollback(); }
@Test public void openSession_with_caching_returns_DbSession_that_does_not_roll_back_on_close_if_any_mutation_call_was_followed_by_rollback_without_parameters() throws SQLException { DbSession dbSession = openSessionAndDoSeveralMutatingAndNeutralCalls(); dbSession.rollback(); dbSession.close(); verify(myBatisDbSession, times(1)).rollback(); }
@Test public void disableCaching_does_not_fail_but_logs_if_closing_MyBatis_session_close_throws_an_exception() { boolean batchOrRegular = random.nextBoolean(); IllegalStateException toBeThrown = new IllegalStateException("Faking MyBatisSession#close failing"); when(myBatis.openSession(batchOrRegular)) .thenReturn(myBatisDbSession) .thenThrow(oneCallTooMuch()); Mockito.doThrow(toBeThrown) .when(myBatisDbSession).close(); underTest.enableCaching(); underTest.openSession(batchOrRegular); underTest.disableCaching(); List<String> errorLogs = logTester.logs(LoggerLevel.ERROR); assertThat(errorLogs) .hasSize(1) .containsOnly("Failed to close " + (batchOrRegular ? "batch" : "regular") + " connection in " + Thread.currentThread()); }
verify(s).close(); reset(s); });
@Test public void unzip_report() throws Exception { logTester.setLevel(LoggerLevel.DEBUG); File reportFile = generateReport(); try (InputStream input = FileUtils.openInputStream(reportFile)) { dbTester.getDbClient().ceTaskInputDao().insert(dbTester.getSession(), TASK_UUID, input); } dbTester.getSession().commit(); dbTester.getSession().close(); underTest.execute(new TestComputationStepContext()); // directory contains the uncompressed report (which contains only metadata.pb in this test) File unzippedDir = reportDirectoryHolder.getDirectory(); assertThat(unzippedDir).isDirectory().exists(); assertThat(unzippedDir.listFiles()).hasSize(1); assertThat(new File(unzippedDir, "metadata.pb")).hasContent("{metadata}"); assertThat(logTester.logs(LoggerLevel.DEBUG)).anyMatch(log -> log.matches("Analysis report is \\d+ bytes uncompressed")); }
@Override public void close() { try { iterator.close(); } finally { DatabaseUtils.closeQuietly(stmt); session.close(); } }
@Override public void start() { DbSession dbSession = dbClient.openSession(false); try { List<MetricDto> metricList = dbClient.metricDao().selectEnabled(dbSession); this.metricsByKey = from(metricList).transform(MetricDtoToMetric.INSTANCE).uniqueIndex(MetricToKey.INSTANCE); this.metricsById = from(metricList).transform(MetricDtoToMetric.INSTANCE).uniqueIndex(MetricToId.INSTANCE); } finally { dbSession.close(); } }