|
| 1 | +package io.ebean.datasource.pool; |
| 2 | + |
| 3 | +import ch.qos.logback.classic.Level; |
| 4 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 5 | +import ch.qos.logback.core.read.ListAppender; |
| 6 | +import io.ebean.datasource.DataSourceBuilder; |
| 7 | +import io.ebean.datasource.DataSourcePool; |
| 8 | +import org.junit.jupiter.api.AfterAll; |
| 9 | +import org.junit.jupiter.api.BeforeAll; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
| 14 | +import java.sql.Connection; |
| 15 | +import java.sql.SQLException; |
| 16 | +import java.sql.SQLSyntaxErrorException; |
| 17 | +import java.util.List; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 22 | + |
| 23 | +/** |
| 24 | + * Tests, if connections are clean when closing them. |
| 25 | + * <p> |
| 26 | + * Test must run with "do not use module path" option in IntelliJ. |
| 27 | + * <p> |
| 28 | + * See also https://github.com/ebean-orm/ebean-datasource/issues/116 for more details |
| 29 | + */ |
| 30 | +class ConnectionPoolCloseTest { |
| 31 | + |
| 32 | + private static ListAppender<ILoggingEvent> logCapture = new ListAppender<>(); |
| 33 | + |
| 34 | + private static DataSourcePool pool = createPool(false, false); |
| 35 | + private static DataSourcePool poolRo = createPool(false, true); |
| 36 | + private static DataSourcePool poolEnforce = createPool(true, false); |
| 37 | + private static DataSourcePool poolEnforceRo = createPool(true, true); |
| 38 | + |
| 39 | + |
| 40 | + @BeforeAll |
| 41 | + static void before() { |
| 42 | + // attach logback appender to capture log messages |
| 43 | + ((ch.qos.logback.classic.Logger) LoggerFactory.getLogger("io.ebean.datasource")).addAppender(logCapture); |
| 44 | + logCapture.start(); |
| 45 | + } |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void beforeEach() { |
| 49 | + logCapture.list.clear(); |
| 50 | + } |
| 51 | + |
| 52 | + @AfterAll |
| 53 | + static void after() { |
| 54 | + logCapture.stop(); |
| 55 | + pool.shutdown(); |
| 56 | + poolRo.shutdown(); |
| 57 | + poolEnforce.shutdown(); |
| 58 | + poolEnforceRo.shutdown(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testNoCommitOrRollback() throws SQLException { |
| 63 | + |
| 64 | + doNoCommitOrRollback(pool); |
| 65 | + assertThat(getAndResetLogWarinings()) |
| 66 | + .hasSize(1) |
| 67 | + .first().asString().startsWith("[WARN] Tried to close a dirty connection at"); |
| 68 | + |
| 69 | + assertThatThrownBy(() -> doNoCommitOrRollback(poolEnforce)).isInstanceOf(AssertionError.class); |
| 70 | + |
| 71 | + doNoCommitOrRollback(poolRo); |
| 72 | + assertThat(getAndResetLogWarinings()).isEmpty(); |
| 73 | + |
| 74 | + doNoCommitOrRollback(poolEnforceRo); |
| 75 | + assertThat(getAndResetLogWarinings()).isEmpty(); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testCommit() throws SQLException { |
| 81 | + doCommit(pool); |
| 82 | + doCommit(poolRo); |
| 83 | + doCommit(poolEnforce); |
| 84 | + doCommit(poolEnforceRo); |
| 85 | + assertThat(getAndResetLogWarinings()).isEmpty(); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void testRollback() throws SQLException { |
| 90 | + doRollback(pool); |
| 91 | + doRollback(poolRo); |
| 92 | + doRollback(poolEnforce); |
| 93 | + doRollback(poolEnforceRo); |
| 94 | + assertThat(getAndResetLogWarinings()).isEmpty(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void testExecuteValidSql() throws SQLException { |
| 99 | + doExecute(pool, "SELECT 1"); |
| 100 | + doExecute(poolRo, "SELECT 1"); |
| 101 | + doExecute(poolEnforce, "SELECT 1"); |
| 102 | + doExecute(poolEnforceRo, "SELECT 1"); |
| 103 | + assertThat(getAndResetLogWarinings()).isEmpty(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void testExecuteInvalidSql() throws SQLException { |
| 108 | + assertThatThrownBy(() -> doExecute(pool, "invalid query")) |
| 109 | + .isInstanceOf(SQLSyntaxErrorException.class) |
| 110 | + .hasNoSuppressedExceptions(); |
| 111 | + // (un)fortunately, we will log a missing rollback, when exception in try-with-resourches is thrown. |
| 112 | + assertThat(getAndResetLogWarinings()) |
| 113 | + .hasSize(1) |
| 114 | + .first().asString().startsWith("[WARN] Tried to close a dirty connection at"); |
| 115 | + |
| 116 | + assertThatThrownBy(() -> doExecute(poolEnforce, "invalid query")) |
| 117 | + .isInstanceOf(SQLSyntaxErrorException.class) |
| 118 | + .hasSuppressedException(new AssertionError("Tried to close a dirty connection. See https://github.com/ebean-orm/ebean-datasource/issues/116 for details.")); |
| 119 | + |
| 120 | + assertThatThrownBy(() -> doExecute(poolRo, "invalid query")) |
| 121 | + .isInstanceOf(SQLSyntaxErrorException.class) |
| 122 | + .hasNoSuppressedExceptions(); |
| 123 | + |
| 124 | + assertThatThrownBy(() -> doExecute(poolEnforceRo, "invalid query")) |
| 125 | + .isInstanceOf(SQLSyntaxErrorException.class) |
| 126 | + .hasNoSuppressedExceptions(); |
| 127 | + assertThat(getAndResetLogWarinings()).isEmpty(); |
| 128 | + } |
| 129 | + |
| 130 | + private static void doNoCommitOrRollback(DataSourcePool pool) throws SQLException { |
| 131 | + try (Connection connection = pool.getConnection()) { |
| 132 | + // we do nothing here. |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private static void doCommit(DataSourcePool pool) throws SQLException { |
| 137 | + try (Connection connection = pool.getConnection()) { |
| 138 | + connection.commit(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static void doRollback(DataSourcePool pool) throws SQLException { |
| 143 | + try (Connection connection = pool.getConnection()) { |
| 144 | + connection.rollback(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private static void doExecute(DataSourcePool pool, String query) throws SQLException { |
| 149 | + try (Connection connection = pool.getConnection()) { |
| 150 | + connection.createStatement().execute(query); |
| 151 | + connection.commit(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private static List<ILoggingEvent> getAndResetLogWarinings() { |
| 156 | + List<ILoggingEvent> warnings = logCapture.list.stream().filter(e -> e.getLevel().levelInt >= Level.WARN_INT).collect(Collectors.toList()); |
| 157 | + logCapture.list.clear(); |
| 158 | + return warnings; |
| 159 | + } |
| 160 | + |
| 161 | + private static DataSourcePool createPool(boolean enforceCleanClose, boolean readOnly) { |
| 162 | + DataSourcePool ret = DataSourceBuilder.create() |
| 163 | + .url("jdbc:h2:mem:tests") |
| 164 | + .username("sa") |
| 165 | + .password("") |
| 166 | + .minConnections(2) |
| 167 | + .maxConnections(4) |
| 168 | + .enforceCleanClose(enforceCleanClose) |
| 169 | + .readOnly(readOnly) |
| 170 | + .build(); |
| 171 | + // clear capturer after create |
| 172 | + logCapture.list.clear(); |
| 173 | + return ret; |
| 174 | + } |
| 175 | +} |
0 commit comments