Skip to content

Commit

Permalink
[CONJ-729] master-slave regression: commit on read-only server
Browse files Browse the repository at this point in the history
Executed only when there is an active transaction on master connection
  • Loading branch information
rusher committed Sep 13, 2019
1 parent 01680d1 commit a41bf18
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,8 @@ public boolean isMasterConnected() {
* @return boolean
*/
public boolean inTransaction() {
if (masterProtocol != null) {
return masterProtocol.inTransaction();
if (currentProtocol != null) {
return currentProtocol.inTransaction();
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@

package org.mariadb.jdbc.failover;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -231,4 +233,32 @@ public void testFailNotOnSlave() throws Throwable {
}
}

@Test
public void commitExecutionOnSlave() throws SQLException {
try (Connection conn = getNewConnection()) {
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE IF NOT EXISTS commitExecution(id int, val varchar(256))");
stmt.execute("TRUNCATE TABLE commitExecution");
stmt.execute("INSERT INTO commitExecution value (1, 'test')");
conn.setAutoCommit(false);
assertFalse(conn.getAutoCommit());
conn.setReadOnly(true);
assertFalse(conn.getAutoCommit());

ResultSet rs = conn.createStatement()
.executeQuery("SELECT COUNT(*) FROM commitExecution");
rs.next();
assertEquals(rs.getInt(1), 1);

conn.setReadOnly(false);
conn.createStatement().execute("INSERT INTO commitExecution value (2, 'test3')");
conn.commit();
conn.setReadOnly(true);
conn.commit();
rs = conn.createStatement().executeQuery("SELECT COUNT(*) FROM commitExecution");
rs.next();
assertEquals(rs.getInt(1), 2);
}
}

}

0 comments on commit a41bf18

Please sign in to comment.