Skip to content

Commit 99b88b9

Browse files
authored
IGNITE-27379 Fix tx support init in partitionAwareness mode (#12587)
1 parent f7c141a commit 99b88b9

File tree

2 files changed

+103
-65
lines changed

2 files changed

+103
-65
lines changed

modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/jdbc/JdbcThinTransactionalSelfTest.java

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
3535
import org.apache.ignite.transactions.TransactionConcurrency;
3636
import org.junit.Test;
37+
import org.junit.runner.RunWith;
38+
import org.junit.runners.Parameterized;
3739

3840
import static java.sql.Connection.TRANSACTION_NONE;
3941
import static java.sql.Connection.TRANSACTION_READ_COMMITTED;
@@ -47,9 +49,20 @@
4749
import static org.apache.ignite.testframework.GridTestUtils.assertThrows;
4850

4951
/** */
52+
@RunWith(Parameterized.class)
5053
public class JdbcThinTransactionalSelfTest extends GridCommonAbstractTest {
54+
/** */
55+
@Parameterized.Parameter
56+
public boolean partitionAwareness;
57+
58+
/** */
59+
@Parameterized.Parameters(name = "partitionAwareness={0}")
60+
public static Object[] parameters() {
61+
return new Object[] {false, true};
62+
}
63+
5164
/** URL. */
52-
public static final String URL = "jdbc:ignite:thin://127.0.0.1";
65+
public static final String URL = "jdbc:ignite:thin://127.0.0.1?";
5366

5467
/** {@inheritDoc} */
5568
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
@@ -71,7 +84,7 @@ public class JdbcThinTransactionalSelfTest extends GridCommonAbstractTest {
7184
/** */
7285
@Test
7386
public void testDatabaseMetadata() throws Exception {
74-
try (Connection conn = DriverManager.getConnection(URL)) {
87+
try (Connection conn = DriverManager.getConnection(url())) {
7588
DatabaseMetaData meta = conn.getMetaData();
7689

7790
assertTrue(meta.supportsTransactions());
@@ -89,7 +102,7 @@ public void testDatabaseMetadata() throws Exception {
89102
/** */
90103
@Test
91104
public void testInvalidHoldability() throws Exception {
92-
try (Connection conn = DriverManager.getConnection(URL)) {
105+
try (Connection conn = DriverManager.getConnection(url())) {
93106
List<TestRunnable> checks = Arrays.asList(
94107
() -> conn.setHoldability(HOLD_CURSORS_OVER_COMMIT),
95108
() -> conn.createStatement(TYPE_FORWARD_ONLY, CONCUR_READ_ONLY, HOLD_CURSORS_OVER_COMMIT),
@@ -116,7 +129,7 @@ public void testInvalidHoldability() throws Exception {
116129
@Test
117130
public void testTransactionConcurrencyProperty() throws Exception {
118131
for (TransactionConcurrency txConcurrency : TransactionConcurrency.values()) {
119-
String url = URL + "?transactionConcurrency=" + txConcurrency;
132+
String url = url("transactionConcurrency=" + txConcurrency);
120133

121134
try (Connection conn = DriverManager.getConnection(url)) {
122135
conn.setAutoCommit(false);
@@ -132,7 +145,7 @@ public void testTransactionConcurrencyProperty() throws Exception {
132145
/** */
133146
@Test
134147
public void testTransactionIsolation() throws Exception {
135-
try (Connection conn = DriverManager.getConnection(URL)) {
148+
try (Connection conn = DriverManager.getConnection(url())) {
136149
assertEquals(TRANSACTION_READ_COMMITTED, conn.getTransactionIsolation());
137150

138151
conn.setTransactionIsolation(TRANSACTION_NONE);
@@ -156,7 +169,7 @@ public void testTransactionIsolation() throws Exception {
156169
/** */
157170
@Test
158171
public void testChangeStreamInsideTransactionThrows() throws Exception {
159-
try (Connection conn = DriverManager.getConnection(URL)) {
172+
try (Connection conn = DriverManager.getConnection(url())) {
160173
conn.setAutoCommit(false);
161174

162175
conn.prepareStatement("SELECT 1").executeQuery();
@@ -176,7 +189,7 @@ public void testChangeStreamInsideTransactionThrows() throws Exception {
176189
/** */
177190
@Test
178191
public void testNoTxInNoTxIsolation() throws Exception {
179-
try (Connection conn = DriverManager.getConnection(URL)) {
192+
try (Connection conn = DriverManager.getConnection(url())) {
180193
conn.setTransactionIsolation(TRANSACTION_NONE);
181194

182195
conn.prepareStatement("SELECT 1").executeQuery();
@@ -188,9 +201,7 @@ public void testNoTxInNoTxIsolation() throws Exception {
188201
/** */
189202
@Test
190203
public void testTransactionLabel() throws Exception {
191-
String url = URL + "?transactionLabel=mylabel";
192-
193-
try (Connection conn = DriverManager.getConnection(url)) {
204+
try (Connection conn = DriverManager.getConnection(url("transactionLabel=mylabel"))) {
194205
conn.setAutoCommit(false);
195206

196207
try (ResultSet rs = conn.prepareStatement("SELECT 1").executeQuery()) {
@@ -205,9 +216,7 @@ public void testTransactionLabel() throws Exception {
205216
public void testTransactionTimeout() throws Exception {
206217
int timeout = 1000;
207218

208-
String url = URL + "?transactionTimeout=" + timeout;
209-
210-
try (Connection conn = DriverManager.getConnection(url)) {
219+
try (Connection conn = DriverManager.getConnection(url("transactionTimeout=" + timeout))) {
211220
conn.setAutoCommit(false);
212221

213222
ResultSet rs = conn.prepareStatement("SELECT 1").executeQuery();
@@ -232,7 +241,7 @@ public void testTransactionTimeout() throws Exception {
232241
public void testStatementsClosedOnTxEnd() throws Exception {
233242
for (boolean commit : new boolean[]{true, false}) {
234243

235-
try (Connection conn = DriverManager.getConnection(URL)) {
244+
try (Connection conn = DriverManager.getConnection(url())) {
236245
conn.setAutoCommit(false);
237246

238247
PreparedStatement stmt0 = conn.prepareStatement("SELECT 1");
@@ -273,7 +282,7 @@ public void testCloseConnectionWithoutCommit() throws Exception {
273282
ResultSet rs0;
274283
ResultSet rs1;
275284

276-
try (Connection conn = DriverManager.getConnection(URL)) {
285+
try (Connection conn = DriverManager.getConnection(url())) {
277286
conn.setAutoCommit(false);
278287

279288
stmt0 = conn.prepareStatement("SELECT 1");
@@ -297,7 +306,7 @@ public void testCloseConnectionWithoutCommit() throws Exception {
297306
/** */
298307
@Test
299308
public void testCreateStatementOnDefaults() throws Exception {
300-
try (Connection conn = DriverManager.getConnection(URL)) {
309+
try (Connection conn = DriverManager.getConnection(url())) {
301310
conn.setAutoCommit(false);
302311

303312
try (Statement stmt = conn.createStatement()) {
@@ -314,4 +323,29 @@ public void testCreateStatementOnDefaults() throws Exception {
314323

315324
}
316325
}
326+
327+
/** */
328+
private String url(String...params) {
329+
String url = URL;
330+
331+
boolean first = true;
332+
333+
if (partitionAwareness) {
334+
url += "partitionAwareness=true";
335+
first = false;
336+
}
337+
338+
if (!F.isEmpty(params)) {
339+
for (String param : params) {
340+
if (first) {
341+
url += param;
342+
first = false;
343+
}
344+
else
345+
url += "&" + param;
346+
}
347+
}
348+
349+
return url;
350+
}
317351
}

modules/core/src/main/java/org/apache/ignite/internal/jdbc/thin/JdbcThinConnection.java

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1985,74 +1985,78 @@ else if (ex instanceof IOException)
19851985
*/
19861986
private IgniteProductVersion connectInBestEffortAffinityMode(
19871987
IgniteProductVersion baseEndpointVer) throws SQLException {
1988-
List<Exception> exceptions = null;
1988+
try {
1989+
List<Exception> exceptions = null;
19891990

1990-
for (int i = 0; i < connProps.getAddresses().length; i++) {
1991-
HostAndPortRange srv = connProps.getAddresses()[i];
1991+
for (int i = 0; i < connProps.getAddresses().length; i++) {
1992+
HostAndPortRange srv = connProps.getAddresses()[i];
19921993

1993-
try {
1994-
InetAddress[] addrs = InetAddress.getAllByName(srv.host());
1994+
try {
1995+
InetAddress[] addrs = InetAddress.getAllByName(srv.host());
19951996

1996-
for (InetAddress addr : addrs) {
1997-
for (int port = srv.portFrom(); port <= srv.portTo(); ++port) {
1998-
try {
1999-
JdbcThinTcpIo cliIo =
2000-
new JdbcThinTcpIo(connProps, new InetSocketAddress(addr, port), ctx, 0);
1997+
for (InetAddress addr : addrs) {
1998+
for (int port = srv.portFrom(); port <= srv.portTo(); ++port) {
1999+
try {
2000+
JdbcThinTcpIo cliIo =
2001+
new JdbcThinTcpIo(connProps, new InetSocketAddress(addr, port), ctx, 0);
20012002

2002-
if (!cliIo.isPartitionAwarenessSupported()) {
2003-
cliIo.close();
2003+
if (!cliIo.isPartitionAwarenessSupported()) {
2004+
cliIo.close();
20042005

2005-
throw new SQLException("Failed to connect to Ignite node [url=" +
2006-
connProps.getUrl() + "]. address = [" + addr + ':' + port + "]." +
2007-
"Node doesn't support partition awareness mode.",
2008-
INTERNAL_ERROR);
2009-
}
2006+
throw new SQLException("Failed to connect to Ignite node [url=" +
2007+
connProps.getUrl() + "]. address = [" + addr + ':' + port + "]." +
2008+
"Node doesn't support partition awareness mode.",
2009+
INTERNAL_ERROR);
2010+
}
20102011

2011-
IgniteProductVersion endpointVer = cliIo.igniteVersion();
2012+
IgniteProductVersion endpointVer = cliIo.igniteVersion();
20122013

2013-
if (baseEndpointVer != null && baseEndpointVer.compareTo(endpointVer) > 0) {
2014-
cliIo.close();
2014+
if (baseEndpointVer != null && baseEndpointVer.compareTo(endpointVer) > 0) {
2015+
cliIo.close();
20152016

2016-
throw new SQLException("Failed to connect to Ignite node [url=" +
2017-
connProps.getUrl() + "], address = [" + addr + ':' + port + "]," +
2018-
"the node version [" + endpointVer + "] " +
2019-
"is smaller than the base one [" + baseEndpointVer + "].",
2020-
INTERNAL_ERROR);
2021-
}
2017+
throw new SQLException("Failed to connect to Ignite node [url=" +
2018+
connProps.getUrl() + "], address = [" + addr + ':' + port + "]," +
2019+
"the node version [" + endpointVer + "] " +
2020+
"is smaller than the base one [" + baseEndpointVer + "].",
2021+
INTERNAL_ERROR);
2022+
}
20222023

2023-
cliIo.timeout(netTimeout);
2024+
cliIo.timeout(netTimeout);
20242025

2025-
JdbcThinTcpIo ioToSameNode = ios.putIfAbsent(cliIo.nodeId(), cliIo);
2026+
JdbcThinTcpIo ioToSameNode = ios.putIfAbsent(cliIo.nodeId(), cliIo);
20262027

2027-
// This can happen if the same node has several IPs or if connection manager background
2028-
// timer task runs concurrently.
2029-
if (ioToSameNode != null)
2030-
cliIo.close();
2031-
else
2032-
connCnt.incrementAndGet();
2028+
// This can happen if the same node has several IPs or if connection manager background
2029+
// timer task runs concurrently.
2030+
if (ioToSameNode != null)
2031+
cliIo.close();
2032+
else
2033+
connCnt.incrementAndGet();
20332034

2034-
return cliIo.igniteVersion();
2035-
}
2036-
catch (Exception exception) {
2037-
if (exceptions == null)
2038-
exceptions = new ArrayList<>();
2035+
return cliIo.igniteVersion();
2036+
}
2037+
catch (Exception exception) {
2038+
if (exceptions == null)
2039+
exceptions = new ArrayList<>();
20392040

2040-
exceptions.add(exception);
2041+
exceptions.add(exception);
2042+
}
20412043
}
20422044
}
20432045
}
2044-
}
2045-
catch (Exception exception) {
2046-
if (exceptions == null)
2047-
exceptions = new ArrayList<>();
2046+
catch (Exception exception) {
2047+
if (exceptions == null)
2048+
exceptions = new ArrayList<>();
20482049

2049-
exceptions.add(exception);
2050+
exceptions.add(exception);
2051+
}
20502052
}
2051-
}
2052-
2053-
handleConnectExceptions(exceptions);
20542053

2055-
isTxAwareQueriesSupported = defaultIo().isTxAwareQueriesSupported();
2054+
handleConnectExceptions(exceptions);
2055+
}
2056+
finally {
2057+
if (!ios.isEmpty())
2058+
isTxAwareQueriesSupported = defaultIo().isTxAwareQueriesSupported();
2059+
}
20562060

20572061
return null;
20582062
}

0 commit comments

Comments
 (0)