Author: shawkins
Date: 2010-06-24 16:04:09 -0400 (Thu, 24 Jun 2010)
New Revision: 2297
Modified:
branches/7.0.x/client/src/main/java/org/teiid/jdbc/XAConnectionImpl.java
branches/7.0.x/client/src/main/java/org/teiid/jdbc/XAResourceImpl.java
branches/7.0.x/client/src/test/java/org/teiid/jdbc/TestXAConnection.java
Log:
TEIID-36 adding a better notification of connection errors in xaconnections.
Modified: branches/7.0.x/client/src/main/java/org/teiid/jdbc/XAConnectionImpl.java
===================================================================
--- branches/7.0.x/client/src/main/java/org/teiid/jdbc/XAConnectionImpl.java 2010-06-24
19:41:21 UTC (rev 2296)
+++ branches/7.0.x/client/src/main/java/org/teiid/jdbc/XAConnectionImpl.java 2010-06-24
20:04:09 UTC (rev 2297)
@@ -39,6 +39,12 @@
import javax.sql.XAConnection;
import javax.transaction.xa.XAResource;
+import org.teiid.client.security.InvalidSessionException;
+import org.teiid.client.util.ExceptionUtil;
+import org.teiid.net.CommunicationException;
+import org.teiid.net.ServerConnection;
+import org.teiid.net.socket.SingleInstanceCommunicationException;
+
/**
* Implementation of XAConnection.
*/
@@ -71,7 +77,25 @@
try {
return method.invoke(this.proxiedConnection, args);
} catch (InvocationTargetException e) {
- //TODO: notify listeners about critical errors
+ Exception ex = ExceptionUtil.getExceptionOfType(e, InvalidSessionException.class);
+ if (ex == null) {
+ ex = ExceptionUtil.getExceptionOfType(e, CommunicationException.class);
+ if (ex instanceof SingleInstanceCommunicationException) {
+ ServerConnection sc = proxiedConnection.getServerConnection();
+ if (sc.isOpen() != null) {
+ ex = null;
+ }
+ }
+ }
+ if (ex != null) {
+ SQLException se = null;
+ if (e.getCause() instanceof SQLException) {
+ se = (SQLException)e.getCause();
+ } else {
+ se = TeiidSQLException.create(e.getCause());
+ }
+ notifyListener(se);
+ }
throw e.getTargetException();
}
}
@@ -93,14 +117,14 @@
}
public Connection getConnection() throws SQLException{
- ConnectionImpl conn = getMMConnection();
+ ConnectionImpl conn = getConnectionImpl();
Connection result =
(Connection)Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]
{Connection.class}, new CloseInterceptor(conn));
return result;
}
- ConnectionImpl getMMConnection() throws SQLException {
+ ConnectionImpl getConnectionImpl() throws SQLException {
if(isClosed){
throw new
SQLException(JDBCPlugin.Util.getString("MMXAConnection.connection_is_closed"));
//$NON-NLS-1$
}
Modified: branches/7.0.x/client/src/main/java/org/teiid/jdbc/XAResourceImpl.java
===================================================================
--- branches/7.0.x/client/src/main/java/org/teiid/jdbc/XAResourceImpl.java 2010-06-24
19:41:21 UTC (rev 2296)
+++ branches/7.0.x/client/src/main/java/org/teiid/jdbc/XAResourceImpl.java 2010-06-24
20:04:09 UTC (rev 2297)
@@ -192,7 +192,7 @@
private ConnectionImpl getMMConnection() throws XAException{
try{
- return this.mmConnection.getMMConnection();
+ return this.mmConnection.getConnectionImpl();
}catch(SQLException e){
throw new XAException(XAException.XAER_RMFAIL);
}
Modified: branches/7.0.x/client/src/test/java/org/teiid/jdbc/TestXAConnection.java
===================================================================
--- branches/7.0.x/client/src/test/java/org/teiid/jdbc/TestXAConnection.java 2010-06-24
19:41:21 UTC (rev 2296)
+++ branches/7.0.x/client/src/test/java/org/teiid/jdbc/TestXAConnection.java 2010-06-24
20:04:09 UTC (rev 2297)
@@ -22,23 +22,23 @@
package org.teiid.jdbc;
+import static org.junit.Assert.*;
+
import java.sql.Connection;
import java.sql.SQLException;
+import javax.sql.ConnectionEvent;
+import javax.sql.ConnectionEventListener;
import javax.transaction.xa.XAResource;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.teiid.client.security.InvalidSessionException;
import org.teiid.client.xa.XidImpl;
-import org.teiid.jdbc.ConnectionImpl;
-import org.teiid.jdbc.StatementImpl;
-import org.teiid.jdbc.XAConnectionImpl;
-import junit.framework.TestCase;
-
-
-
-public class TestXAConnection extends TestCase {
+public class TestXAConnection {
- public void testConnectionClose() throws Exception {
+ @Test public void testConnectionClose() throws Exception {
final ConnectionImpl mmConn = TestConnection.getMMConnection();
@@ -68,5 +68,27 @@
assertTrue(stmt.isClosed());
assertTrue(conn.getAutoCommit());
}
+
+ @Test public void testNotification() throws Exception {
+ XAConnectionImpl xaConn = new XAConnectionImpl(new XAConnectionImpl.ConnectionSource()
{
+ //## JDBC4.0-begin ##
+ @Override
+ //## JDBC4.0-end ##
+ public ConnectionImpl createConnection() throws SQLException {
+ ConnectionImpl c = Mockito.mock(ConnectionImpl.class);
+ Mockito.doThrow(new SQLException(new InvalidSessionException())).when(c).commit();
+ return c;
+ }
+ });
+ ConnectionEventListener cel = Mockito.mock(ConnectionEventListener.class);
+ xaConn.addConnectionEventListener(cel);
+ Connection c = xaConn.getConnection();
+ try {
+ c.commit();
+ } catch (SQLException e) {
+
+ }
+ Mockito.verify(cel).connectionErrorOccurred((ConnectionEvent) Mockito.anyObject());
+ }
}
Show replies by date