use of com.mysql.cj.xdevapi.DbDocImpl in project aws-mysql-jdbc by awslabs.
the class RowLockingTest method testFindRowLockingValid.
/**
* START collection.find() tests
*
* @throws Exception
*/
@Test
public void testFindRowLockingValid() throws Exception {
assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.0")), "MySQL 8.0+ is required to run this test.");
int i = 0;
try {
DbDoc[] jsonlist = new DbDocImpl[10];
for (i = 1; i <= 10; i++) {
DbDoc newDoc2 = new DbDocImpl();
newDoc2.add("F1", new JsonNumber().setValue(String.valueOf(i)));
newDoc2.add("F2", new JsonString().setValue("Field-1-Data-" + i));
newDoc2.add("F3", new JsonNumber().setValue(String.valueOf(10 * (i) + 0.1234)));
jsonlist[i - 1] = newDoc2;
newDoc2 = null;
}
this.collection.add(jsonlist).execute();
assertEquals((10), this.collection.count());
/* Two threads with same conditions, select for update in one and update in second */
this.CheckFlag = 0;
initException = new Throwable[2];
FindRowLock[] Thrd = new FindRowLock[2];
Thrd[0] = new FindRowLock(1, 1, 0, 5, "$.F1 = ?");
Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
Thrd[0].start();
Thrd[1] = new FindRowLock(2, 0, 1, 5, "$.F1 = ?");
Thrd[1].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(1));
Thrd[1].start();
for (i = 0; i < 2; i++) {
Thrd[i].join();
}
for (i = 0; i < 2; i++) {
if (initException[i] != null) {
throw new RuntimeException(initException[i]);
}
}
/* Two threads with same conditions, select for share in one and update in second */
this.CheckFlag = 0;
initException = new Throwable[2];
Thrd[0] = new FindRowLock(1, 2, 0, 5, "$.F1 = ?");
Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
Thrd[0].start();
Thrd[1] = new FindRowLock(2, 0, 1, 5, "$.F1 = ?");
Thrd[1].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(1));
Thrd[1].start();
for (i = 0; i < 2; i++) {
Thrd[i].join();
}
for (i = 0; i < 2; i++) {
if (initException[i] != null) {
throw new RuntimeException(initException[i]);
}
}
/* Two threads with same conditions selecting multiple records, select for update in one and update in second */
this.CheckFlag = 0;
initException = new Throwable[2];
Thrd[0] = new FindRowLock(1, 2, 0, 5, "$.F1 < ?");
Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
Thrd[0].start();
Thrd[1] = new FindRowLock(2, 0, 1, 5, "$.F1 < ?");
Thrd[1].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(1));
Thrd[1].start();
for (i = 0; i < 2; i++) {
Thrd[i].join();
}
for (i = 0; i < 2; i++) {
if (initException[i] != null) {
throw new RuntimeException(initException[i]);
}
}
/* Two threads with same conditions selecting multiple rows, select for share in one and update in second */
this.CheckFlag = 0;
initException = new Throwable[2];
Thrd[0] = new FindRowLock(1, 1, 0, 5, "$.F1 < ?");
Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
Thrd[0].start();
Thrd[1] = new FindRowLock(2, 0, 1, 5, "$.F1 < ?");
Thrd[1].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(1));
Thrd[1].start();
for (i = 0; i < 2; i++) {
Thrd[i].join();
}
for (i = 0; i < 2; i++) {
if (initException[i] != null) {
throw new RuntimeException(initException[i]);
}
}
/* Two threads with same conditions, select for update in one and select for share in second */
this.CheckFlag = 0;
initException = new Throwable[2];
Thrd[0] = new FindRowLock(1, 2, 0, 5, "$.F1 < ?");
Thrd[0].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(0));
Thrd[0].start();
Thrd[1] = new FindRowLock(1, 1, 1, 5, "$.F1 < ?");
Thrd[1].setUncaughtExceptionHandler(new MyUncaughtExceptionHandler(1));
Thrd[1].start();
for (i = 0; i < 2; i++) {
Thrd[i].join();
}
for (i = 0; i < 2; i++) {
if (initException[i] != null) {
throw new RuntimeException(initException[i]);
}
}
} catch (RuntimeException e) {
System.out.print("**************RuntimeException: " + i);
System.out.println(e.getMessage());
throw e;
} catch (InterruptedException e) {
System.out.print("InterruptedException: " + i);
System.out.println(e.getMessage());
throw e;
}
}
use of com.mysql.cj.xdevapi.DbDocImpl in project aws-mysql-jdbc by awslabs.
the class TableInsertTest method jsonInsert.
@Test
public void jsonInsert() throws IOException {
try {
sqlUpdate("drop table if exists jsonInsert");
sqlUpdate("create table jsonInsert (_id varchar(32), doc JSON)");
Table table = this.schema.getTable("jsonInsert");
table.insert("_id", "doc").values(1, "{\"x\":\"1\"}").execute();
table.insert().values(2, "{\"x\":\"2\"}").execute();
Map<String, Object> row = new HashMap<>();
row.put("_id", 3);
row.put("doc", "{\"x\":\"3\"}");
table.insert(row).execute();
DbDoc doc = new DbDocImpl().add("firstName", new JsonString().setValue("Georgia"));
doc.add("middleName", new JsonString().setValue("Totto"));
doc.add("lastName", new JsonString().setValue("O'Keeffe"));
table.insert("_id", "doc").values(4, doc).execute();
RowResult rows = table.select("_id, doc").orderBy("_id").execute();
Row r = rows.next();
assertEquals("1", r.getString("_id"));
assertEquals(JsonParser.parseDoc(new StringReader("{\"x\":\"1\"}")).toString(), r.getDbDoc("doc").toString());
r = rows.next();
assertEquals("2", r.getString("_id"));
assertEquals("{\"x\": \"2\"}", r.getString("doc"));
r = rows.next();
assertEquals("3", r.getString("_id"));
assertEquals("{\"x\": \"3\"}", r.getString("doc"));
r = rows.next();
assertEquals("4", r.getString("_id"));
assertEquals(doc.toString(), r.getDbDoc("doc").toString());
} finally {
sqlUpdate("drop table if exists jsonInsert");
}
}
Aggregations