Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 782b643

Browse files
committed
Catching timeout during createStatement to prevent potential JDBC bug
1 parent c4539ea commit 782b643

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

src/main/java/org/utplsql/api/TestRunner.java

+36-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.utplsql.api.compatibility.CompatibilityProxy;
66
import org.utplsql.api.db.DatabaseInformation;
77
import org.utplsql.api.db.DefaultDatabaseInformation;
8+
import org.utplsql.api.exception.OracleCreateStatmenetStuckException;
89
import org.utplsql.api.exception.SomeTestsFailedException;
910
import org.utplsql.api.exception.UtPLSQLNotInstalledException;
1011
import org.utplsql.api.reporter.DocumentationReporter;
@@ -16,6 +17,7 @@
1617
import java.sql.SQLException;
1718
import java.util.ArrayList;
1819
import java.util.List;
20+
import java.util.concurrent.*;
1921

2022
/**
2123
* Created by Vinicius Avellar on 12/04/2017.
@@ -124,6 +126,21 @@ private void delayedAddReporters() {
124126
}
125127
}
126128

129+
private void handleException(Throwable e) throws SQLException {
130+
if (e instanceof SQLException) {
131+
SQLException sqlException = (SQLException) e;
132+
if (sqlException.getErrorCode() == SomeTestsFailedException.ERROR_CODE) {
133+
throw new SomeTestsFailedException(sqlException.getMessage(), e);
134+
} else if (((SQLException) e).getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE) {
135+
throw new UtPLSQLNotInstalledException(sqlException);
136+
} else {
137+
throw sqlException;
138+
}
139+
} else {
140+
throw new SQLException("Unknown exception, wrapping: " + e.getMessage(), e);
141+
}
142+
}
143+
127144
public void run(Connection conn) throws SQLException {
128145

129146
logger.info("TestRunner initialized");
@@ -156,19 +173,29 @@ public void run(Connection conn) throws SQLException {
156173
options.reporterList.add(new DocumentationReporter().init(conn));
157174
}
158175

159-
try (TestRunnerStatement testRunnerStatement = compatibilityProxy.getTestRunnerStatement(options, conn)) {
176+
ExecutorService executor = Executors.newSingleThreadExecutor();
177+
Callable<TestRunnerStatement> callable = () -> compatibilityProxy.getTestRunnerStatement(options, conn);
178+
Future<TestRunnerStatement> future = executor.submit(callable);
179+
180+
// We want to leave the statement open in case of stuck scenario
181+
TestRunnerStatement testRunnerStatement = null;
182+
try {
183+
testRunnerStatement = future.get(2, TimeUnit.SECONDS);
160184
logger.info("Running tests");
161185
testRunnerStatement.execute();
162186
logger.info("Running tests finished.");
163-
} catch (SQLException e) {
164-
if (e.getErrorCode() == SomeTestsFailedException.ERROR_CODE) {
165-
throw new SomeTestsFailedException(e.getMessage(), e);
166-
} else if (e.getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE) {
167-
throw new UtPLSQLNotInstalledException(e);
168-
} else {
169-
throw e;
170-
}
187+
testRunnerStatement.close();
188+
} catch (TimeoutException e) {
189+
executor.shutdownNow();
190+
throw new OracleCreateStatmenetStuckException(e);
191+
} catch (ExecutionException e) {
192+
handleException(e.getCause());
193+
} catch (Exception e) {
194+
if (testRunnerStatement != null) testRunnerStatement.close();
195+
handleException(e);
171196
}
197+
198+
executor.shutdown();
172199
}
173200

174201
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.utplsql.api.exception;
2+
3+
import java.sql.SQLException;
4+
5+
public class OracleCreateStatmenetStuckException extends SQLException {
6+
public OracleCreateStatmenetStuckException(Throwable cause) {
7+
super("Oracle driver stuck during creating the TestRunner statement. Retry.", cause);
8+
}
9+
}

0 commit comments

Comments
 (0)