Skip to content
Permalink
Browse files

[misc] code cleaning, second round

(cherry picked from commit 6fa80dc)
  • Loading branch information...
rusher committed Jan 23, 2019
1 parent add2498 commit 7037eec930337776565025b532e96c29a69ff547
@@ -115,11 +115,9 @@
* one of
* <code>Statement.RETURN_GENERATED_KEYS</code>
* or <code>Statement.NO_GENERATED_KEYS</code>
* @throws SQLException if cannot retrieve auto increment value
*/
public BasePrepareStatement(MariaDbConnection connection, int resultSetScrollType,
int resultSetConcurrency, int autoGeneratedKeys)
throws SQLException {
int resultSetConcurrency, int autoGeneratedKeys) {
super(connection, resultSetScrollType, resultSetConcurrency);
this.noBackslashEscapes = protocol.noBackslashEscapes();
this.useFractionalSeconds = options.useFractionalSeconds;
@@ -283,7 +283,7 @@ public void setPropertiesTest() throws SQLException {
}

@Test
public void setLoginTimeOut() throws SQLException {
public void setLoginTimeOut() {
MariaDbDataSource ds = new MariaDbDataSource(hostname == null ? "localhost" : hostname, port,
database);
assertEquals(30, ds.getLoginTimeout());
@@ -110,7 +110,7 @@ public void setCharacterStream() throws SQLException, IOException {


@Test
public void position() throws SQLException {
public void position() {
MariaDbClob clob = new MariaDbClob(bytes);
assertEquals(4, clob.position("de", 2));

@@ -52,78 +52,76 @@

package org.mariadb.jdbc;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TransactionTest extends BaseTest {

/**
* Tables initialisation.
*
* @throws SQLException
* exception
*/
@Before
public void before() throws SQLException {
if (testSingleHost) {
Statement stmt = sharedConnection.createStatement();
stmt.execute("drop table if exists tx_fore_key");
stmt.execute("drop table if exists tx_prim_key");
createTable("tx_prim_key", "id int not null primary key", "engine=innodb");
createTable("tx_fore_key",
"id int not null primary key, id_ref int not null, "
+ "foreign key (id_ref) references tx_prim_key(id) on delete restrict on update restrict",
"engine=innodb");
}
/**
* Tables initialisation.
*
* @throws SQLException exception
*/
@Before
public void before() throws SQLException {
if (testSingleHost) {
Statement stmt = sharedConnection.createStatement();
stmt.execute("drop table if exists tx_fore_key");
stmt.execute("drop table if exists tx_prim_key");
createTable("tx_prim_key", "id int not null primary key", "engine=innodb");
createTable("tx_fore_key",
"id int not null primary key, id_ref int not null, "
+ "foreign key (id_ref) references tx_prim_key(id) on delete restrict on update restrict",
"engine=innodb");
}
}

/**
* Clean up created tables.
*
* @throws SQLException
* exception
*/
@After
public void after() throws SQLException {
if (testSingleHost) {
Statement stmt = sharedConnection.createStatement();
stmt.execute("drop table if exists tx_fore_key");
stmt.execute("drop table if exists tx_prim_key");
}
/**
* Clean up created tables.
*
* @throws SQLException exception
*/
@After
public void after() throws SQLException {
if (testSingleHost) {
Statement stmt = sharedConnection.createStatement();
stmt.execute("drop table if exists tx_fore_key");
stmt.execute("drop table if exists tx_prim_key");
}
}

@Test
public void testProperRollback() throws Exception {
Statement st = sharedConnection.createStatement();
st.executeUpdate("insert into tx_prim_key(id) values(32)");
st.executeUpdate("insert into tx_fore_key(id, id_ref) values(42, 32)");
@Test
public void testProperRollback() throws Exception {
try (Statement st = sharedConnection.createStatement()) {
st.executeUpdate("insert into tx_prim_key(id) values(32)");
st.executeUpdate("insert into tx_fore_key(id, id_ref) values(42, 32)");
}

// 2. try to delete entry in Primary table in a transaction - which will fail due
// foreign key.
sharedConnection.setAutoCommit(false);
try (Statement st = sharedConnection.createStatement()) {
st.executeUpdate("delete from tx_prim_key where id = 32");
sharedConnection.commit();
fail("Expected SQLException");
} catch (SQLException e) {
// This exception is expected
assertTrue(e.getMessage().contains("a foreign key constraint fails"));
sharedConnection.rollback();
}

// 2. try to delete entry in Primary table in a transaction - wich will fail due
// foreign key.
sharedConnection.setAutoCommit(false);
try {
st.executeUpdate("delete from tx_prim_key where id = 32");
sharedConnection.commit();
fail("Expected SQLException");
} catch (SQLException e) {
// This exception is expected
assertTrue(e.getMessage().contains("a foreign key constraint fails"));
sharedConnection.rollback();
}
Connection conn2 = openNewConnection(connUri);
Statement st2 = conn2.createStatement();
st2.setQueryTimeout(3);
st2.executeUpdate("delete from tx_fore_key where id = 42");
st2.executeUpdate("delete from tx_prim_key where id = 32");
conn2.close();
try (Connection conn2 = openNewConnection(connUri); Statement st = conn2.createStatement()) {
st.setQueryTimeout(30000);
st.executeUpdate("delete from tx_fore_key where id = 42");
st.executeUpdate("delete from tx_prim_key where id = 32");
}
}

}

0 comments on commit 7037eec

Please sign in to comment.
You can’t perform that action at this time.