List:Commits« Previous MessageNext Message »
From:mmatthews Date:February 28 2008 5:05am
Subject:Connector/J commit: r6750 - in branches/branch_5_1: . src/com/mysql/jdbc src/testsuite/simple
View as plain text  
Added:
   branches/branch_5_1/src/testsuite/simple/TestLifecycleInterceptor.java
Modified:
   branches/branch_5_1/CHANGES
   branches/branch_5_1/src/com/mysql/jdbc/ConnectionImpl.java
   branches/branch_5_1/src/com/mysql/jdbc/ConnectionLifecycleInterceptor.java
   branches/branch_5_1/src/com/mysql/jdbc/MysqlIO.java
   branches/branch_5_1/src/testsuite/simple/ConnectionTest.java
Log:
The ConnectionLifecycleInterceptor interface now has callback methods for
      transaction initiation (transactionBegun()), and completion 
      (transactionCompleted()), as reported by the *server* (i.e. 
      calling Connection.setAutoCommit(false) will not trigger 
      transactionBegun() being called, however the first statement
      which causes a transaction to start on the server will cause
      transactionBegun() to be called *after* the statement has been processed
      on the server).

Modified: branches/branch_5_1/CHANGES
===================================================================
--- branches/branch_5_1/CHANGES	2008-02-27 17:38:07 UTC (rev 6749)
+++ branches/branch_5_1/CHANGES	2008-02-28 04:05:18 UTC (rev 6750)
@@ -149,6 +149,15 @@
     - Fixed BUG#33162 - NullPointerException instead of SQLException 
       thrown for ResultSet.getTimestamp() when not positioned on a
       row. 
+    
+    - The ConnectionLifecycleInterceptor interface now has callback methods for
+      transaction initiation (transactionBegun()), and completion 
+      (transactionCompleted()), as reported by the *server* (i.e. 
+      calling Connection.setAutoCommit(false) will not trigger 
+      transactionBegun() being called, however the first statement
+      which causes a transaction to start on the server will cause
+      transactionBegun() to be called *after* the statement has been processed
+      on the server).
       
 10-09-07 - Version 5.1.5
 

Modified: branches/branch_5_1/src/com/mysql/jdbc/ConnectionImpl.java
===================================================================
--- branches/branch_5_1/src/com/mysql/jdbc/ConnectionImpl.java	2008-02-27 17:38:07 UTC
(rev 6749)
+++ branches/branch_5_1/src/com/mysql/jdbc/ConnectionImpl.java	2008-02-28 04:05:18 UTC
(rev 6750)
@@ -5497,4 +5497,30 @@
 	public void initializeExtension(Extension ex) throws SQLException {
 		ex.init(this, this.props);
 	}
+	
+	protected void transactionBegun() throws SQLException {
+		if (this.connectionLifecycleInterceptors != null) {
+			IterateBlock iter = new IterateBlock(this.connectionLifecycleInterceptors.iterator())
{
+
+				void forEach(Object each) throws SQLException {
+					((ConnectionLifecycleInterceptor)each).transactionBegun();
+				}
+			};
+			
+			iter.doForAll();
+		}
+	}
+	
+	protected void transactionCompleted() throws SQLException {
+		if (this.connectionLifecycleInterceptors != null) {
+			IterateBlock iter = new IterateBlock(this.connectionLifecycleInterceptors.iterator())
{
+
+				void forEach(Object each) throws SQLException {
+					((ConnectionLifecycleInterceptor)each).transactionCompleted();
+				}
+			};
+			
+			iter.doForAll();
+		}
+	}
 }
\ No newline at end of file

Modified: branches/branch_5_1/src/com/mysql/jdbc/ConnectionLifecycleInterceptor.java
===================================================================
--- branches/branch_5_1/src/com/mysql/jdbc/ConnectionLifecycleInterceptor.java	2008-02-27
17:38:07 UTC (rev 6749)
+++ branches/branch_5_1/src/com/mysql/jdbc/ConnectionLifecycleInterceptor.java	2008-02-28
04:05:18 UTC (rev 6750)
@@ -112,4 +112,16 @@
 	 * @throws SQLException if an error occurs
 	 */
 	public abstract boolean setCatalog(String catalog) throws SQLException;
+	
+	/**
+	 * Called when the driver has been told by the server that a transaction
+	 * is now in progress (when one has not been currently in progress).
+	 */
+	public abstract boolean transactionBegun() throws SQLException;
+	
+	/**
+	 * Called when the driver has been told by the server that a transaction
+	 * has completed, and no transaction is currently in progress.
+	 */
+	public abstract boolean transactionCompleted() throws SQLException;
 }

Modified: branches/branch_5_1/src/com/mysql/jdbc/MysqlIO.java
===================================================================
--- branches/branch_5_1/src/com/mysql/jdbc/MysqlIO.java	2008-02-27 17:38:07 UTC (rev 6749)
+++ branches/branch_5_1/src/com/mysql/jdbc/MysqlIO.java	2008-02-28 04:05:18 UTC (rev 6750)
@@ -216,6 +216,7 @@
     protected int serverCapabilities;
     private int serverMajorVersion = 0;
     private int serverMinorVersion = 0;
+    private int oldServerStatus = 0;
     private int serverStatus = 0;
     private int serverSubMinorVersion = 0;
     private int warningCount = 0;
@@ -1169,6 +1170,7 @@
             /* New protocol with 16 bytes to describe server characteristics */
             this.serverCharsetIndex = buf.readByte() & 0xff;
             this.serverStatus = buf.readInt();
+            checkTransactionState(0);
             buf.setPosition(position + 16);
 
             String seedPart2 = buf.readString("ASCII");
@@ -1553,8 +1555,12 @@
 															// sendCommand()
 							}
 
+							this.oldServerStatus = this.serverStatus;
+							
 							this.serverStatus = (this.mysqlInput.read() & 0xff)
 									| ((this.mysqlInput.read() & 0xff) << 8);
+							checkTransactionState(oldServerStatus);
+							
 							remaining -= 2;
 
 							if (remaining > 0) {
@@ -1842,6 +1848,7 @@
             // Clear serverStatus...this value is guarded by an
             // external mutex, as you can only ever be processing
             // one command at a time
+            this.oldServerStatus = this.serverStatus;
             this.serverStatus = 0;
             this.hadWarnings = false;
             this.warningCount = 0;
@@ -2565,8 +2572,11 @@
             }
 
             if (this.use41Extensions) {
+            	// oldStatus set in sendCommand()
                 this.serverStatus = resultPacket.readInt();
 
+                checkTransactionState(oldServerStatus);
+                
                 this.warningCount = resultPacket.readInt();
 
                 if (this.warningCount > 0) {
@@ -2684,7 +2694,9 @@
                 this.hadWarnings = true; // this is a 'latch', it's reset by
sendCommand()
             }
 
+            this.oldServerStatus = this.serverStatus;
             this.serverStatus = rowPacket.readInt();
+            checkTransactionState(oldServerStatus);
 
             if (this.profileSql) {
                 this.queryNoIndexUsed = (this.serverStatus &
@@ -4545,4 +4557,15 @@
 	protected int getCommandCount() {
 		return this.commandCount;
 	}
+	
+	private void checkTransactionState(int oldStatus) throws SQLException {
+		boolean previouslyInTrans = ((oldStatus & SERVER_STATUS_IN_TRANS) != 0);
+		boolean currentlyInTrans = ((this.serverStatus & SERVER_STATUS_IN_TRANS) != 0);
+
+		if (previouslyInTrans && !currentlyInTrans) {
+			this.connection.transactionCompleted();
+		} else if (!previouslyInTrans && currentlyInTrans) {
+			this.connection.transactionBegun();
+		}
+	}
 }
\ No newline at end of file

Modified: branches/branch_5_1/src/testsuite/simple/ConnectionTest.java
===================================================================
--- branches/branch_5_1/src/testsuite/simple/ConnectionTest.java	2008-02-27 17:38:07 UTC
(rev 6749)
+++ branches/branch_5_1/src/testsuite/simple/ConnectionTest.java	2008-02-28 04:05:18 UTC
(rev 6750)
@@ -1651,4 +1651,30 @@
 		}
 		
 	}
+	
+	public void testLifecyleInterceptor() throws Exception {
+		createTable("testLifecycleInterceptor", "(field1 int) ENGINE=InnoDB");
+		Connection liConn = null;
+		
+		try {
+			liConn =
getConnectionWithProps("connectionLifecycleInterceptors=testsuite.simple.TestLifecycleInterceptor");
+			liConn.setAutoCommit(false);
+		
+			liConn.createStatement().executeUpdate("INSERT INTO testLifecycleInterceptor VALUES
(1)");
+			liConn.commit();
+			assertEquals(TestLifecycleInterceptor.transactionsBegun, 1);
+			assertEquals(TestLifecycleInterceptor.transactionsCompleted, 1);
+			liConn.createStatement().executeQuery("SELECT * FROM testLifecycleInterceptor");
+			assertEquals(TestLifecycleInterceptor.transactionsBegun, 2);
+			// implicit commit
+			liConn.createStatement().executeUpdate("CREATE TABLE testLifecycleFoo (field1 int)");
+			assertEquals(TestLifecycleInterceptor.transactionsCompleted, 2);
+		} finally {
+			if (liConn != null) {
+				liConn.createStatement().executeUpdate("DROP TABLE IF EXISTS testLifecycleFoo");
+				liConn.close();
+			}
+		}
+		
+	}
 }
\ No newline at end of file

Added: branches/branch_5_1/src/testsuite/simple/TestLifecycleInterceptor.java
===================================================================
--- branches/branch_5_1/src/testsuite/simple/TestLifecycleInterceptor.java	               
        (rev 0)
+++ branches/branch_5_1/src/testsuite/simple/TestLifecycleInterceptor.java	2008-02-28
04:05:18 UTC (rev 6750)
@@ -0,0 +1,67 @@
+/**
+ * 
+ */
+package testsuite.simple;
+
+import java.sql.SQLException;
+import java.sql.Savepoint;
+import java.util.Properties;
+
+import com.mysql.jdbc.ConnectionLifecycleInterceptor;
+
+public class TestLifecycleInterceptor implements ConnectionLifecycleInterceptor {
+	static int transactionsBegun = 0;
+	static int transactionsCompleted = 0;
+	
+	public void close() throws SQLException {
+		// TODO Auto-generated method stub
+		
+	}
+
+	public boolean commit() throws SQLException {
+		// TODO Auto-generated method stub
+		return true;
+	}
+
+	public boolean rollback() throws SQLException {
+		// TODO Auto-generated method stub
+		return true;
+	}
+
+	public boolean rollback(Savepoint s) throws SQLException {
+		// TODO Auto-generated method stub
+		return true;
+	}
+
+	public boolean setAutoCommit(boolean flag) throws SQLException {
+		// TODO Auto-generated method stub
+		return true;
+	}
+
+	public boolean setCatalog(String catalog) throws SQLException {
+		// TODO Auto-generated method stub
+		return true;
+	}
+
+	public boolean transactionBegun() throws SQLException {
+		transactionsBegun++;
+		return true;
+	}
+
+	public boolean transactionCompleted() throws SQLException {
+		transactionsCompleted++;
+		return true;
+	}
+
+	public void destroy() {
+		// TODO Auto-generated method stub
+		
+	}
+
+	public void init(com.mysql.jdbc.Connection conn, Properties props)
+			throws SQLException {
+		// TODO Auto-generated method stub
+		
+	}
+	
+}
\ No newline at end of file


Property changes on:
branches/branch_5_1/src/testsuite/simple/TestLifecycleInterceptor.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Thread
Connector/J commit: r6750 - in branches/branch_5_1: . src/com/mysql/jdbc src/testsuite/simplemmatthews28 Feb