#2782 - Support MariaDB jdbc driver update - Batched JDBC SQLException translation, SqlRow BLOB reading

- Batched JDBC SQLException translation - Need to dig into nested exception to get cause error state
- SqlRow BLOB reading - BLOB can come as LONGVARBINARY (TestRawSqlBuilder)
- Fix test for history using system time (TestHistoryInsert)
- Fix SqlQueryCancelTest
- Fix TestSqlUpdateExceptions
This commit is contained in:
Rob Bygrave committed 2022-08-17 17:25:28 +12:00
1 parent ff45f9ab63
commit 51d2f52d19
8 files changed
+28 -21

No files matched your search

@@ -32,7 +32,12 @@ public class SqlCodeTranslator implements SqlExceptionTranslator {
} }
private DataErrorType getErrorType(SQLException e) { private DataErrorType getErrorType(SQLException e) {
DataErrorType errorType = map.get(e.getSQLState()); String sqlState = e.getSQLState();
while (sqlState == null && e.getCause() instanceof SQLException) {
e = (SQLException)e.getCause();
sqlState = e.getSQLState();
}
DataErrorType errorType = map.get(sqlState);
if (errorType == null) { if (errorType == null) {
// fall back to error code // fall back to error code
errorType = map.get(String.valueOf(e.getErrorCode())); errorType = map.get(String.valueOf(e.getErrorCode()));
@@ -58,6 +58,7 @@ public final class DRawSqlService implements SpiRawSqlService {
ret.put(name, resultSet.getString(i)); ret.put(name, resultSet.getString(i));
break; break;
case Types.LONGVARBINARY:
case Types.BLOB: case Types.BLOB:
ret.put(name, resultSet.getBytes(i)); ret.put(name, resultSet.getBytes(i));
break; break;
@@ -298,7 +298,7 @@ public class TestRawSqlBuilder extends BaseTestCase {
} }
@Test @Test
public void testBLobClosedConnection() throws Exception { void testBLobClosedConnection() throws Exception {
final PersistentFileContent pfc = new PersistentFileContent(); final PersistentFileContent pfc = new PersistentFileContent();
final byte[] bytes = "This is the blob as String".getBytes(StandardCharsets.UTF_8); final byte[] bytes = "This is the blob as String".getBytes(StandardCharsets.UTF_8);
pfc.setContent(bytes); pfc.setContent(bytes);
@@ -5,11 +5,12 @@ import io.ebean.test.containers.MariaDBContainer;
public class StartMariaDb { public class StartMariaDb {
public static void main(String[] args) { public static void main(String[] args) {
MariaDBContainer.builder("10.5") MariaDBContainer.builder("10.6")
.dbName("unit") .dbName("unit")
.user("unit") .user("unit")
.password("unit") .password("unit")
.port(5306)
.build() .build()
.startWithDropCreate(); .start();
} }
} }
@@ -15,19 +15,23 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class TestHistoryInsert extends BaseTestCase { class TestHistoryInsert extends BaseTestCase {
private final Logger logger = LoggerFactory.getLogger(TestHistoryInsert.class); private final Logger logger = LoggerFactory.getLogger(TestHistoryInsert.class);
private Timestamp currentDbSystemTime() {
return DB.sqlQuery("select current_timestamp(6)").mapToScalar(Timestamp.class).findOne();
}
/** /**
* Looks like we MUST use useLegacyDatetimeCode=false ... in order for * Looks like we MUST use useLegacyDatetimeCode=false ... in order for
* the correct server timezone to be honored by the MariaDB JDBC driver. * the correct server timezone to be honored by the MariaDB JDBC driver.
*/ */
@Test @Test
@ForPlatform({Platform.MARIADB}) @ForPlatform({Platform.MARIADB})
public void mariadb_simple_history() { void mariadb_simple_history() {
Timestamp t0 = new Timestamp(System.currentTimeMillis()); Timestamp t0 = currentDbSystemTime();
littleSleep(50); littleSleep(50);
User user = new User(); User user = new User();
@@ -35,19 +39,19 @@ public class TestHistoryInsert extends BaseTestCase {
user.setEmail("one@email.com"); user.setEmail("one@email.com");
user.setPasswordHash("someHash"); user.setPasswordHash("someHash");
DB.save(user); DB.save(user);
Timestamp t1 = new Timestamp(System.currentTimeMillis()); Timestamp t1 = currentDbSystemTime();
littleSleep(100); littleSleep(100);
user.setName("NotJim"); user.setName("NotJim");
user.save(); user.save();
Timestamp t2 = new Timestamp(System.currentTimeMillis()); Timestamp t2 = currentDbSystemTime();
littleSleep(100); littleSleep(100);
user.setName("NotJimV2"); user.setName("NotJimV2");
user.setEmail("two@email.com"); user.setEmail("two@email.com");
user.save(); user.save();
littleSleep(50); littleSleep(50);
Timestamp t3 = new Timestamp(System.currentTimeMillis()); Timestamp t3 = currentDbSystemTime();
List<Version<User>> versions = DB.find(User.class).setId(user.getId()).findVersionsBetween(t0, t3); List<Version<User>> versions = DB.find(User.class).setId(user.getId()).findVersionsBetween(t0, t3);
assertThat(versions).hasSize(3); assertThat(versions).hasSize(3);
@@ -59,7 +59,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
* *
* @author Roland Praml, FOCONIS AG * @author Roland Praml, FOCONIS AG
*/ */
public class SqlQueryCancelTest extends BaseTestCase { class SqlQueryCancelTest extends BaseTestCase {
private final int timing = 20; private final int timing = 20;
@@ -191,17 +191,13 @@ public class SqlQueryCancelTest extends BaseTestCase {
} }
@Test @Test
public void cancelOrmDtoDuringIterate() { void cancelOrmDtoDuringIterate() {
DtoQuery<EBasicDto> query = DB.find(EBasic.class).select("id,status").asDto(EBasicDto.class); DtoQuery<EBasicDto> query = DB.find(EBasic.class).select("id,status").asDto(EBasicDto.class);
QueryIterator<EBasicDto> iter = query.findIterate(); QueryIterator<EBasicDto> iter = query.findIterate();
assertThat(iter.hasNext()).isTrue(); assertThat(iter.hasNext()).isTrue();
query.cancel(); query.cancel();
if (isMariaDB()) { assertThat(iter.next()).isNotNull();
assertThrows(PersistenceException.class, iter::next);
} else {
assertThat(iter.next()).isNotNull();
}
// We might have 100 entities in a buffer. So we must iterate through all. // We might have 100 entities in a buffer. So we must iterate through all.
assertThatThrownBy(() -> { assertThatThrownBy(() -> {
@@ -9,7 +9,7 @@ import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
public class TestSqlUpdateExceptions extends BaseTestCase { class TestSqlUpdateExceptions extends BaseTestCase {
private String sql = "insert into uuone (id, name, version) values (?,?,?)"; private String sql = "insert into uuone (id, name, version) values (?,?,?)";
@@ -63,7 +63,7 @@ public class TestSqlUpdateExceptions extends BaseTestCase {
} }
@Test @Test
public void duplicateKey_inBatch() { void duplicateKey_inBatch() {
UUID id = UUID.randomUUID(); UUID id = UUID.randomUUID();
assertThrows(DuplicateKeyException.class, () -> { assertThrows(DuplicateKeyException.class, () -> {
try (Transaction transaction = DB.beginTransaction()) { try (Transaction transaction = DB.beginTransaction()) {
@@ -85,7 +85,7 @@ public class TestSqlUpdateExceptions extends BaseTestCase {
} }
@Test @Test
public void duplicateKey_executeBatch() { void duplicateKey_executeBatch() {
UUID id = UUID.randomUUID(); UUID id = UUID.randomUUID();
assertThrows(DuplicateKeyException.class, () -> { assertThrows(DuplicateKeyException.class, () -> {
try (Transaction transaction = DB.beginTransaction()) { try (Transaction transaction = DB.beginTransaction()) {
@@ -138,7 +138,7 @@ datasource.mysql.url=jdbc:mysql://127.0.0.1:4306/unit
datasource.mariadb.username=unit datasource.mariadb.username=unit
datasource.mariadb.password=unit datasource.mariadb.password=unit
datasource.mariadb.url=jdbc:mariadb://localhost:4306/unit?useLegacyDatetimeCode=false datasource.mariadb.url=jdbc:mariadb://localhost:5306/unit?useLegacyDatetimeCode=false
datasource.cockroach.username=root datasource.cockroach.username=root
datasource.cockroach.password= datasource.cockroach.password=