| 1 |
/* |
| 2 |
* Copyright (C) 2013, 2014 Brett Wooldridge |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.zaxxer.hikari.pool; |
| 18 |
|
| 19 |
import static com.zaxxer.hikari.pool.TestElf.newHikariConfig; |
| 20 |
import static com.zaxxer.hikari.pool.TestElf.newHikariDataSource; |
| 21 |
import static org.junit.Assert.assertEquals; |
| 22 |
import static org.junit.Assert.assertSame; |
| 23 |
import static org.junit.Assert.assertTrue; |
| 24 |
import static org.junit.Assert.assertFalse; |
| 25 |
|
| 26 |
import java.sql.Connection; |
| 27 |
import java.sql.ResultSet; |
| 28 |
import java.sql.SQLException; |
| 29 |
import java.sql.Statement; |
| 30 |
|
| 31 |
import org.junit.Test; |
| 32 |
|
| 33 |
import com.zaxxer.hikari.HikariConfig; |
| 34 |
import com.zaxxer.hikari.HikariDataSource; |
| 35 |
import com.zaxxer.hikari.util.UtilityElf; |
| 36 |
|
| 37 |
public class ConnectionStateTest |
| 38 |
{ |
| 39 |
@Test |
| 40 |
public void testAutoCommit() throws SQLException |
| 41 |
{ |
| 42 |
try (HikariDataSource ds = newHikariDataSource()) { |
| 43 |
ds.setAutoCommit(true); |
| 44 |
ds.setMinimumIdle(1); |
| 45 |
ds.setMaximumPoolSize(1); |
| 46 |
ds.setConnectionTestQuery("VALUES 1"); |
| 47 |
ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 48 |
ds.addDataSourceProperty("user", "bar"); |
| 49 |
ds.addDataSourceProperty("password", "secret"); |
| 50 |
ds.addDataSourceProperty("url", "baf"); |
| 51 |
ds.addDataSourceProperty("loginTimeout", "10"); |
| 52 |
|
| 53 |
try (Connection connection = ds.getConnection()) { |
| 54 |
Connection unwrap = connection.unwrap(Connection.class); |
| 55 |
connection.setAutoCommit(false); |
| 56 |
connection.close(); |
| 57 |
|
| 58 |
assertTrue(unwrap.getAutoCommit()); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Test |
| 64 |
public void testTransactionIsolation() throws SQLException |
| 65 |
{ |
| 66 |
try (HikariDataSource ds = newHikariDataSource()) { |
| 67 |
ds.setTransactionIsolation("TRANSACTION_READ_COMMITTED"); |
| 68 |
ds.setMinimumIdle(1); |
| 69 |
ds.setMaximumPoolSize(1); |
| 70 |
ds.setConnectionTestQuery("VALUES 1"); |
| 71 |
ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 72 |
|
| 73 |
try (Connection connection = ds.getConnection()) { |
| 74 |
Connection unwrap = connection.unwrap(Connection.class); |
| 75 |
connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); |
| 76 |
connection.close(); |
| 77 |
|
| 78 |
assertEquals(Connection.TRANSACTION_READ_COMMITTED, unwrap.getTransactionIsolation()); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void testIsolation() throws Exception |
| 85 |
{ |
| 86 |
HikariConfig config = newHikariConfig(); |
| 87 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 88 |
config.setTransactionIsolation("TRANSACTION_REPEATABLE_READ"); |
| 89 |
config.validate(); |
| 90 |
|
| 91 |
int transactionIsolation = UtilityElf.getTransactionIsolation(config.getTransactionIsolation()); |
| 92 |
assertSame(Connection.TRANSACTION_REPEATABLE_READ, transactionIsolation); |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
public void testReadOnly() throws Exception |
| 97 |
{ |
| 98 |
try (HikariDataSource ds = newHikariDataSource()) { |
| 99 |
ds.setCatalog("test"); |
| 100 |
ds.setMinimumIdle(1); |
| 101 |
ds.setMaximumPoolSize(1); |
| 102 |
ds.setConnectionTestQuery("VALUES 1"); |
| 103 |
ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 104 |
|
| 105 |
try (Connection connection = ds.getConnection()) { |
| 106 |
Connection unwrap = connection.unwrap(Connection.class); |
| 107 |
connection.setReadOnly(true); |
| 108 |
connection.close(); |
| 109 |
|
| 110 |
assertFalse(unwrap.isReadOnly()); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
@Test |
| 116 |
public void testCatalog() throws SQLException |
| 117 |
{ |
| 118 |
try (HikariDataSource ds = newHikariDataSource()) { |
| 119 |
ds.setCatalog("test"); |
| 120 |
ds.setMinimumIdle(1); |
| 121 |
ds.setMaximumPoolSize(1); |
| 122 |
ds.setConnectionTestQuery("VALUES 1"); |
| 123 |
ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 124 |
|
| 125 |
try (Connection connection = ds.getConnection()) { |
| 126 |
Connection unwrap = connection.unwrap(Connection.class); |
| 127 |
connection.setCatalog("other"); |
| 128 |
connection.close(); |
| 129 |
|
| 130 |
assertEquals("test", unwrap.getCatalog()); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
@Test |
| 136 |
public void testCommitTracking() throws SQLException |
| 137 |
{ |
| 138 |
try (HikariDataSource ds = newHikariDataSource()) { |
| 139 |
ds.setAutoCommit(false); |
| 140 |
ds.setMinimumIdle(1); |
| 141 |
ds.setMaximumPoolSize(1); |
| 142 |
ds.setConnectionTestQuery("VALUES 1"); |
| 143 |
ds.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 144 |
|
| 145 |
try (Connection connection = ds.getConnection()) { |
| 146 |
Statement statement = connection.createStatement(); |
| 147 |
statement.execute("SELECT something"); |
| 148 |
assertTrue(TestElf.getConnectionCommitDirtyState(connection)); |
| 149 |
|
| 150 |
connection.commit(); |
| 151 |
assertFalse(TestElf.getConnectionCommitDirtyState(connection)); |
| 152 |
|
| 153 |
statement.execute("SELECT something", Statement.NO_GENERATED_KEYS); |
| 154 |
assertTrue(TestElf.getConnectionCommitDirtyState(connection)); |
| 155 |
|
| 156 |
connection.rollback(); |
| 157 |
assertFalse(TestElf.getConnectionCommitDirtyState(connection)); |
| 158 |
|
| 159 |
ResultSet resultSet = statement.executeQuery("SELECT something"); |
| 160 |
assertTrue(TestElf.getConnectionCommitDirtyState(connection)); |
| 161 |
|
| 162 |
connection.rollback(null); |
| 163 |
assertFalse(TestElf.getConnectionCommitDirtyState(connection)); |
| 164 |
|
| 165 |
resultSet.updateRow(); |
| 166 |
assertTrue(TestElf.getConnectionCommitDirtyState(connection)); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |