Author: shawkins
Date: 2012-03-05 15:30:07 -0500 (Mon, 05 Mar 2012)
New Revision: 3913
Modified:
branches/7.7.x/client/src/main/java/org/teiid/jdbc/ConnectionImpl.java
branches/7.7.x/client/src/main/java/org/teiid/jdbc/StatementImpl.java
branches/7.7.x/client/src/test/java/org/teiid/jdbc/TestStatement.java
Log:
TEIID-1865 switching to a thread safe statement collection and updating TestStatement
Modified: branches/7.7.x/client/src/main/java/org/teiid/jdbc/ConnectionImpl.java
===================================================================
--- branches/7.7.x/client/src/main/java/org/teiid/jdbc/ConnectionImpl.java 2012-03-03
21:37:04 UTC (rev 3912)
+++ branches/7.7.x/client/src/main/java/org/teiid/jdbc/ConnectionImpl.java 2012-03-05
20:30:07 UTC (rev 3913)
@@ -22,28 +22,16 @@
package org.teiid.jdbc;
-import java.sql.Array;
-import java.sql.Blob;
-import java.sql.CallableStatement;
-import java.sql.Clob;
-import java.sql.Connection;
-import java.sql.NClob;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLClientInfoException;
-import java.sql.SQLException;
-import java.sql.SQLWarning;
-import java.sql.SQLXML;
-import java.sql.Savepoint;
-import java.sql.Statement;
-import java.sql.Struct;
+import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -90,7 +78,7 @@
private boolean inLocalTxn;
// collection of all open statements on this connection
- private Collection<StatementImpl> statements = new
ArrayList<StatementImpl>();
+ private Collection<StatementImpl> statements = Collections.newSetFromMap(new
ConcurrentHashMap<StatementImpl, Boolean>());
// cached DatabaseMetadata
private DatabaseMetaDataImpl dbmm;
Modified: branches/7.7.x/client/src/main/java/org/teiid/jdbc/StatementImpl.java
===================================================================
--- branches/7.7.x/client/src/main/java/org/teiid/jdbc/StatementImpl.java 2012-03-03
21:37:04 UTC (rev 3912)
+++ branches/7.7.x/client/src/main/java/org/teiid/jdbc/StatementImpl.java 2012-03-05
20:30:07 UTC (rev 3913)
@@ -28,17 +28,7 @@
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLWarning;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.TimeZone;
+import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
@@ -224,7 +214,7 @@
* Reset all per-execution state - this should be done before executing
* a new command.
*/
- protected void resetExecutionState() throws SQLException {
+ protected synchronized void resetExecutionState() throws SQLException {
this.currentRequestID = -1;
this.currentPlanDescription = null;
@@ -253,7 +243,7 @@
batchedUpdates.add(sql);
}
- public void cancel() throws SQLException {
+ public synchronized void cancel() throws SQLException {
/* Defect 19848 - Mark the statement cancelled before sending the CANCEL
request.
* Otherwise, it's possible get into a race where the server response is
quicker
* than the exception in the exception in the conditionalWait(), which results
in
@@ -612,7 +602,7 @@
return rs;
}
- private void postReceiveResults(RequestMessage reqMessage,
+ private synchronized void postReceiveResults(RequestMessage reqMessage,
ResultsMessage resultsMsg) throws TeiidSQLException, SQLException {
commandStatus = State.DONE;
// warnings thrown
@@ -862,7 +852,7 @@
/**
* Ends the command and sets the status to TIMED_OUT.
*/
- protected void timeoutOccurred() {
+ protected synchronized void timeoutOccurred() {
if (this.commandStatus != State.RUNNING) {
return;
}
Modified: branches/7.7.x/client/src/test/java/org/teiid/jdbc/TestStatement.java
===================================================================
--- branches/7.7.x/client/src/test/java/org/teiid/jdbc/TestStatement.java 2012-03-03
21:37:04 UTC (rev 3912)
+++ branches/7.7.x/client/src/test/java/org/teiid/jdbc/TestStatement.java 2012-03-05
20:30:07 UTC (rev 3913)
@@ -28,9 +28,12 @@
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
+import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
import org.teiid.client.DQP;
import org.teiid.client.RequestMessage;
import org.teiid.client.ResultsMessage;
@@ -121,19 +124,36 @@
@Test public void testAsynchTimeout() throws Exception {
ConnectionImpl conn = Mockito.mock(ConnectionImpl.class);
- StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
+ final StatementImpl statement = new StatementImpl(conn, ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
statement.setQueryTimeoutMS(1);
DQP dqp = Mockito.mock(DQP.class);
Mockito.stub(statement.getDQP()).toReturn(dqp);
+ final AtomicInteger counter = new AtomicInteger();
+ Mockito.stub(dqp.cancelRequest(0)).toAnswer(new Answer<Boolean>() {
+ @Override
+ public Boolean answer(InvocationOnMock invocation) throws Throwable {
+ synchronized (statement) {
+ counter.incrementAndGet();
+ statement.notifyAll();
+ }
+ return true;
+ }
+ });
ResultsFuture<ResultsMessage> future = new
ResultsFuture<ResultsMessage>();
Mockito.stub(dqp.executeRequest(Mockito.anyLong(), (RequestMessage)
Mockito.anyObject())).toReturn(future);
statement.submitExecute("select 'hello world'");
- Thread.sleep(300);
- Mockito.verify(dqp).cancelRequest(0);
+ synchronized (statement) {
+ while (counter.get() != 1) {
+ statement.wait();
+ }
+ }
statement.setQueryTimeoutMS(1);
statement.submitExecute("select 'hello world'");
- Thread.sleep(300);
- Mockito.verify(dqp, Mockito.times(2)).cancelRequest(0);
+ synchronized (statement) {
+ while (counter.get() != 2) {
+ statement.wait();
+ }
+ }
}
@Test public void testTimeoutProperty() throws Exception {
Show replies by date