use of com.mysql.cj.xdevapi.SessionFactory in project aws-mysql-jdbc by awslabs.
the class CollectionFindTest method testCollectionRowLocks.
@Test
public void testCollectionRowLocks() throws Exception {
assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.3")), "MySQL 8.0.3+ is required to run this test.");
this.collection.add("{\"_id\":\"1\", \"a\":1}").execute();
this.collection.add("{\"_id\":\"2\", \"a\":1}").execute();
this.collection.add("{\"_id\":\"3\", \"a\":1}").execute();
Session session1 = null;
Session session2 = null;
try {
session1 = new SessionFactory().getSession(this.testProperties);
Collection col1 = session1.getDefaultSchema().getCollection(this.collectionName);
session2 = new SessionFactory().getSession(this.testProperties);
Collection col2 = session2.getDefaultSchema().getCollection(this.collectionName);
// test1: Shared Lock
session1.startTransaction();
col1.find("_id = '1'").lockShared().execute();
session2.startTransaction();
// should return immediately
col2.find("_id = '2'").lockShared().execute();
// should return immediately
CompletableFuture<DocResult> res1 = col2.find("_id = '1'").lockShared().executeAsync();
res1.get(5, TimeUnit.SECONDS);
assertTrue(res1.isDone());
session1.rollback();
session2.rollback();
// test2: Shared Lock after Exclusive
session1.startTransaction();
col1.find("_id = '1'").lockExclusive().execute();
session2.startTransaction();
// should return immediately
col2.find("_id = '2'").lockShared().execute();
// session2 blocks
CompletableFuture<DocResult> res2 = col2.find("_id = '1'").lockShared().executeAsync();
assertThrows(TimeoutException.class, new Callable<Void>() {
public Void call() throws Exception {
res2.get(5, TimeUnit.SECONDS);
return null;
}
});
// session2 should unblock now
session1.rollback();
res2.get(5, TimeUnit.SECONDS);
assertTrue(res2.isDone());
session2.rollback();
// test3: Exclusive after Shared
session1.startTransaction();
col1.find("_id = '1'").lockShared().execute();
col1.find("_id = '3'").lockShared().execute();
session2.startTransaction();
// should return immediately
col2.find("_id = '2'").lockExclusive().execute();
// should return immediately
col2.find("_id = '3'").lockShared().execute();
// session2 should block
CompletableFuture<DocResult> res3 = col2.find("_id = '1'").lockExclusive().executeAsync();
assertThrows(TimeoutException.class, new Callable<Void>() {
public Void call() throws Exception {
res3.get(5, TimeUnit.SECONDS);
return null;
}
});
// session2 should unblock now
session1.rollback();
res3.get(5, TimeUnit.SECONDS);
assertTrue(res3.isDone());
session2.rollback();
// test4: Exclusive after Exclusive
session1.startTransaction();
col1.find("_id = '1'").lockExclusive().execute();
session2.startTransaction();
// should return immediately
col2.find("_id = '2'").lockExclusive().execute();
// session2 should block
CompletableFuture<DocResult> res4 = col2.find("_id = '1'").lockExclusive().executeAsync();
assertThrows(TimeoutException.class, new Callable<Void>() {
public Void call() throws Exception {
res4.get(5, TimeUnit.SECONDS);
return null;
}
});
// session2 should unblock now
session1.rollback();
res4.get(5, TimeUnit.SECONDS);
assertTrue(res4.isDone());
session2.rollback();
} finally {
if (session1 != null) {
session1.close();
}
if (session2 != null) {
session2.close();
}
}
}
use of com.mysql.cj.xdevapi.SessionFactory in project aws-mysql-jdbc by awslabs.
the class CollectionFindTest method testCollectionFindStress_002.
/* 64k length key */
@Test
public void testCollectionFindStress_002() throws Exception {
int i = 0, maxrec = 5;
int maxLen = 1024 * 64 - 1;
SqlResult res1 = null;
Session tmpSess = null;
Row r = null;
int defXPackLen = 0;
int defPackLen = 0;
try {
tmpSess = new SessionFactory().getSession(this.baseUrl);
res1 = tmpSess.sql("show variables like 'mysqlx_max_allowed_packet'").execute();
r = res1.next();
defXPackLen = Integer.parseInt(r.getString("Value"));
res1 = tmpSess.sql("show variables like 'max_allowed_packet'").execute();
r = res1.next();
defPackLen = Integer.parseInt(r.getString("Value"));
tmpSess.sql("set Global mysqlx_max_allowed_packet=128*1024*1024 ").execute();
tmpSess.sql("set Global max_allowed_packet=128*1024*1024 ").execute();
String s1 = "";
/* max+1 key length --> Expect error */
s1 = buildString(maxLen + 1, 'q');
DbDoc[] jsonlist = new DbDocImpl[maxrec];
for (i = 0; i < maxrec; i++) {
DbDoc newDoc2 = new DbDocImpl();
newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1 + 1000)));
newDoc2.add("F1", new JsonNumber().setValue(String.valueOf(i + 1)));
newDoc2.add(s1, new JsonString().setValue("Data_1" + i));
jsonlist[i] = newDoc2;
newDoc2 = null;
}
assertThrows(XProtocolError.class, "ERROR 3151 \\(22032\\) The JSON object contains a key name that is too long\\.", () -> this.collection.add(jsonlist).execute());
/* With Max Keysize */
s1 = buildString(maxLen - 1, 'q');
for (i = 0; i < maxrec; i++) {
DbDoc newDoc2 = new DbDocImpl();
newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1 + 1000)));
newDoc2.add(s1 + "1", new JsonNumber().setValue(String.valueOf(i + 1)));
newDoc2.add(s1 + "2", new JsonString().setValue("Data_1" + i));
// jsonlist[i]=newDoc2;
this.collection.add(newDoc2).execute();
newDoc2 = null;
}
// coll.add(jsonlist).execute();
DocResult docs0 = this.collection.find("$._id= '1001'").fields("$._id as _id, $." + s1 + "1 as " + s1 + "X, $." + s1 + "2 as " + s1 + "Y").execute();
DbDoc doc0 = docs0.next();
assertEquals(String.valueOf(1 + 1000), (((JsonString) doc0.get("_id")).getString()));
} finally {
if (tmpSess != null) {
tmpSess.sql("set Global mysqlx_max_allowed_packet=" + defXPackLen).execute();
tmpSess.sql("set Global max_allowed_packet=" + defPackLen).execute();
tmpSess.close();
}
}
}
use of com.mysql.cj.xdevapi.SessionFactory in project aws-mysql-jdbc by awslabs.
the class CollectionFindTest method testCollectionFindStress_003.
/* Large Data */
@Test
public // @Ignore("Wait for 1M Data issue Fix in Plugin")
void testCollectionFindStress_003() throws Exception {
int i = 0, maxrec = 5;
int maxLen = 1024 * 1024 + 4;
SqlResult res1 = null;
Session tmpSess = null;
Row r = null;
int defPackLen = 0;
int defXPackLen = 0;
try {
tmpSess = new SessionFactory().getSession(this.baseUrl);
res1 = tmpSess.sql("show variables like 'mysqlx_max_allowed_packet'").execute();
r = res1.next();
defXPackLen = Integer.parseInt(r.getString("Value"));
res1 = tmpSess.sql("show variables like 'max_allowed_packet'").execute();
r = res1.next();
defPackLen = Integer.parseInt(r.getString("Value"));
tmpSess.sql("set Global mysqlx_max_allowed_packet=128*1024*1024 ").execute();
tmpSess.sql("set Global max_allowed_packet=128*1024*1024 ").execute();
((SessionImpl) this.session).getSession().getProtocol().setMaxAllowedPacket(128 * 1024 * 1024);
String s1 = "";
/* maxLen Data length */
s1 = buildString(maxLen + 1, 'q');
for (i = 0; i < maxrec; i++) {
DbDoc newDoc2 = new DbDocImpl();
newDoc2.add("_id", new JsonString().setValue(String.valueOf(i + 1 + 1000)));
newDoc2.add("F1", new JsonString().setValue(s1 + i));
newDoc2.add("F2", new JsonString().setValue(s1 + i));
this.collection.add(newDoc2).execute();
newDoc2 = null;
}
DocResult docs0 = this.collection.find("$._id= '1001'").fields("$._id as _id, $.F1 as f1, $.F2 as f2").execute();
DbDoc doc0 = docs0.next();
assertEquals(String.valueOf(1 + 1000), (((JsonString) doc0.get("_id")).getString()));
} finally {
if (tmpSess != null) {
tmpSess.sql("set Global mysqlx_max_allowed_packet=" + defXPackLen).execute();
tmpSess.sql("set Global max_allowed_packet=" + defPackLen).execute();
tmpSess.close();
}
}
}
use of com.mysql.cj.xdevapi.SessionFactory in project aws-mysql-jdbc by awslabs.
the class CollectionModifyTest method testPreparedStatements.
@Test
public void testPreparedStatements() {
assumeTrue(mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.14")), "MySQL 8.0.14+ is required to run this test.");
try {
// Prepare test data.
testPreparedStatementsResetData();
SessionFactory sf = new SessionFactory();
/*
* Test common usage.
*/
Session testSession = sf.getSession(this.testProperties);
int sessionThreadId = getThreadId(testSession);
assertPreparedStatementsCount(sessionThreadId, 0, 1);
assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
Collection testCol1 = testSession.getDefaultSchema().getCollection(this.collectionName + "_1");
Collection testCol2 = testSession.getDefaultSchema().getCollection(this.collectionName + "_2");
Collection testCol3 = testSession.getDefaultSchema().getCollection(this.collectionName + "_3");
Collection testCol4 = testSession.getDefaultSchema().getCollection(this.collectionName + "_4");
// Initialize several ModifyStatement objects.
// Modify all.
ModifyStatement testModify1 = testCol1.modify("true").set("ord", expr("$.ord * 10"));
// Criteria with one placeholder.
ModifyStatement testModify2 = testCol2.modify("$.ord >= :n").set("ord", expr("$.ord * 10"));
// Criteria with same placeholder repeated.
ModifyStatement testModify3 = testCol3.modify("$.ord >= :n AND $.ord <= :n + 1").set("ord", expr("$.ord * 10"));
// Criteria with multiple placeholders.
ModifyStatement testModify4 = testCol4.modify("$.ord >= :n AND $.ord <= :m").set("ord", expr("$.ord * 10"));
assertPreparedStatementsCountsAndId(testSession, 0, testModify1, 0, -1);
assertPreparedStatementsCountsAndId(testSession, 0, testModify2, 0, -1);
assertPreparedStatementsCountsAndId(testSession, 0, testModify3, 0, -1);
assertPreparedStatementsCountsAndId(testSession, 0, testModify4, 0, -1);
// A. Set binds: 1st execute -> non-prepared.
assertTestPreparedStatementsResult(testModify1.execute(), 4, testCol1.getName(), 10, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 0, testModify1, 0, -1);
assertTestPreparedStatementsResult(testModify2.bind("n", 2).execute(), 3, testCol2.getName(), 1, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 0, testModify2, 0, -1);
assertTestPreparedStatementsResult(testModify3.bind("n", 2).execute(), 2, testCol3.getName(), 1, 20, 30, 4);
assertPreparedStatementsCountsAndId(testSession, 0, testModify3, 0, -1);
assertTestPreparedStatementsResult(testModify4.bind("n", 2).bind("m", 3).execute(), 2, testCol4.getName(), 1, 20, 30, 4);
assertPreparedStatementsCountsAndId(testSession, 0, testModify4, 0, -1);
assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
testPreparedStatementsResetData();
// B. Set sort resets execution count: 1st execute -> non-prepared.
assertTestPreparedStatementsResult(testModify1.sort("$._id").execute(), 4, testCol1.getName(), 10, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 0, testModify1, 0, -1);
assertTestPreparedStatementsResult(testModify2.sort("$._id").execute(), 3, testCol2.getName(), 1, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 0, testModify2, 0, -1);
assertTestPreparedStatementsResult(testModify3.sort("$._id").execute(), 2, testCol3.getName(), 1, 20, 30, 4);
assertPreparedStatementsCountsAndId(testSession, 0, testModify3, 0, -1);
assertTestPreparedStatementsResult(testModify4.sort("$._id").execute(), 2, testCol4.getName(), 1, 20, 30, 4);
assertPreparedStatementsCountsAndId(testSession, 0, testModify4, 0, -1);
assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
testPreparedStatementsResetData();
// C. Set binds reuse statement: 2nd execute -> prepare + execute.
assertTestPreparedStatementsResult(testModify1.execute(), 4, testCol1.getName(), 10, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 1, testModify1, 1, 1);
assertTestPreparedStatementsResult(testModify2.bind("n", 3).execute(), 2, testCol2.getName(), 1, 2, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 2, testModify2, 2, 1);
assertTestPreparedStatementsResult(testModify3.bind("n", 3).execute(), 2, testCol3.getName(), 1, 2, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 3, testModify3, 3, 1);
assertTestPreparedStatementsResult(testModify4.bind("m", 4).execute(), 3, testCol4.getName(), 1, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 4, testModify4, 4, 1);
assertPreparedStatementsStatusCounts(testSession, 4, 4, 0);
testPreparedStatementsResetData();
// D. Set binds reuse statement: 3rd execute -> execute.
assertTestPreparedStatementsResult(testModify1.execute(), 4, testCol1.getName(), 10, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 4, testModify1, 1, 2);
assertTestPreparedStatementsResult(testModify2.bind("n", 4).execute(), 1, testCol2.getName(), 1, 2, 3, 40);
assertPreparedStatementsCountsAndId(testSession, 4, testModify2, 2, 2);
assertTestPreparedStatementsResult(testModify3.bind("n", 1).execute(), 2, testCol3.getName(), 10, 20, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 4, testModify3, 3, 2);
assertTestPreparedStatementsResult(testModify4.bind("m", 2).execute(), 1, testCol4.getName(), 1, 20, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 4, testModify4, 4, 2);
assertPreparedStatementsStatusCounts(testSession, 4, 8, 0);
testPreparedStatementsResetData();
// E. Set new values deallocates and resets execution count: 1st execute -> deallocate + non-prepared.
assertTestPreparedStatementsResult(testModify1.set("ord", expr("$.ord * 100")).execute(), 4, testCol1.getName(), 100, 200, 300, 400);
assertPreparedStatementsCountsAndId(testSession, 3, testModify1, 0, -1);
assertTestPreparedStatementsResult(testModify2.set("ord", expr("$.ord * 100")).execute(), 1, testCol2.getName(), 1, 2, 3, 400);
assertPreparedStatementsCountsAndId(testSession, 2, testModify2, 0, -1);
assertTestPreparedStatementsResult(testModify3.set("ord", expr("$.ord * 100")).execute(), 2, testCol3.getName(), 100, 200, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 1, testModify3, 0, -1);
assertTestPreparedStatementsResult(testModify4.set("ord", expr("$.ord * 100")).execute(), 1, testCol4.getName(), 1, 200, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 0, testModify4, 0, -1);
assertPreparedStatementsStatusCounts(testSession, 4, 8, 4);
testPreparedStatementsResetData();
// F. No Changes: 2nd execute -> prepare + execute.
assertTestPreparedStatementsResult(testModify1.execute(), 4, testCol1.getName(), 100, 200, 300, 400);
assertPreparedStatementsCountsAndId(testSession, 1, testModify1, 1, 1);
assertTestPreparedStatementsResult(testModify2.execute(), 1, testCol2.getName(), 1, 2, 3, 400);
assertPreparedStatementsCountsAndId(testSession, 2, testModify2, 2, 1);
assertTestPreparedStatementsResult(testModify3.execute(), 2, testCol3.getName(), 100, 200, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 3, testModify3, 3, 1);
assertTestPreparedStatementsResult(testModify4.execute(), 1, testCol4.getName(), 1, 200, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 4, testModify4, 4, 1);
assertPreparedStatementsStatusCounts(testSession, 8, 12, 4);
testPreparedStatementsResetData();
// G. Set limit for the first time deallocates and re-prepares: 1st execute -> re-prepare + execute.
assertTestPreparedStatementsResult(testModify1.limit(1).execute(), 1, testCol1.getName(), 100, 2, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 4, testModify1, 1, 1);
assertTestPreparedStatementsResult(testModify2.limit(1).execute(), 1, testCol2.getName(), 1, 2, 3, 400);
assertPreparedStatementsCountsAndId(testSession, 4, testModify2, 2, 1);
assertTestPreparedStatementsResult(testModify3.limit(1).execute(), 1, testCol3.getName(), 100, 2, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 4, testModify3, 3, 1);
assertTestPreparedStatementsResult(testModify4.limit(1).execute(), 1, testCol4.getName(), 1, 200, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 4, testModify4, 4, 1);
assertPreparedStatementsStatusCounts(testSession, 12, 16, 8);
testPreparedStatementsResetData();
// H. Set limit reuse prepared statement: 2nd execute -> execute.
assertTestPreparedStatementsResult(testModify1.limit(2).execute(), 2, testCol1.getName(), 100, 200, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 4, testModify1, 1, 2);
assertTestPreparedStatementsResult(testModify2.limit(2).execute(), 1, testCol2.getName(), 1, 2, 3, 400);
assertPreparedStatementsCountsAndId(testSession, 4, testModify2, 2, 2);
assertTestPreparedStatementsResult(testModify3.limit(2).execute(), 2, testCol3.getName(), 100, 200, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 4, testModify3, 3, 2);
assertTestPreparedStatementsResult(testModify4.limit(2).execute(), 1, testCol4.getName(), 1, 200, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 4, testModify4, 4, 2);
assertPreparedStatementsStatusCounts(testSession, 12, 20, 8);
testPreparedStatementsResetData();
// I. Set sort deallocates and resets execution count, set limit has no effect: 1st execute -> deallocate + non-prepared.
assertTestPreparedStatementsResult(testModify1.sort("$._id").limit(1).execute(), 1, testCol1.getName(), 100, 2, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 3, testModify1, 0, -1);
assertTestPreparedStatementsResult(testModify2.sort("$._id").limit(1).execute(), 1, testCol2.getName(), 1, 2, 3, 400);
assertPreparedStatementsCountsAndId(testSession, 2, testModify2, 0, -1);
assertTestPreparedStatementsResult(testModify3.sort("$._id").limit(1).execute(), 1, testCol3.getName(), 100, 2, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 1, testModify3, 0, -1);
assertTestPreparedStatementsResult(testModify4.sort("$._id").limit(1).execute(), 1, testCol4.getName(), 1, 200, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 0, testModify4, 0, -1);
assertPreparedStatementsStatusCounts(testSession, 12, 20, 12);
testPreparedStatementsResetData();
// J. Set limit reuse statement: 2nd execute -> prepare + execute.
assertTestPreparedStatementsResult(testModify1.limit(2).execute(), 2, testCol1.getName(), 100, 200, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 1, testModify1, 1, 1);
assertTestPreparedStatementsResult(testModify2.limit(2).execute(), 1, testCol2.getName(), 1, 2, 3, 400);
assertPreparedStatementsCountsAndId(testSession, 2, testModify2, 2, 1);
assertTestPreparedStatementsResult(testModify3.limit(2).execute(), 2, testCol3.getName(), 100, 200, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 3, testModify3, 3, 1);
assertTestPreparedStatementsResult(testModify4.limit(2).execute(), 1, testCol4.getName(), 1, 200, 3, 4);
assertPreparedStatementsCountsAndId(testSession, 4, testModify4, 4, 1);
assertPreparedStatementsStatusCounts(testSession, 16, 24, 12);
testPreparedStatementsResetData();
testSession.close();
// Prepared statements won't live past the closing of the session.
assertPreparedStatementsCount(sessionThreadId, 0, 10);
/*
* Test falling back onto non-prepared statements.
*/
testSession = sf.getSession(this.testProperties);
int origMaxPrepStmtCount = this.session.sql("SELECT @@max_prepared_stmt_count").execute().fetchOne().getInt(0);
try {
// Allow preparing only one more statement.
this.session.sql("SET GLOBAL max_prepared_stmt_count = ?").bind(getPreparedStatementsCount() + 1).execute();
sessionThreadId = getThreadId(testSession);
assertPreparedStatementsCount(sessionThreadId, 0, 1);
assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
testCol1 = testSession.getDefaultSchema().getCollection(this.collectionName + "_1");
testCol2 = testSession.getDefaultSchema().getCollection(this.collectionName + "_2");
testModify1 = testCol1.modify("true").set("ord", expr("$.ord * 10"));
testModify2 = testCol2.modify("true").set("ord", expr("$.ord * 10"));
// 1st execute -> don't prepare.
assertTestPreparedStatementsResult(testModify1.execute(), 4, testCol1.getName(), 10, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 0, testModify1, 0, -1);
assertTestPreparedStatementsResult(testModify2.execute(), 4, testCol2.getName(), 10, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 0, testModify2, 0, -1);
assertPreparedStatementsStatusCounts(testSession, 0, 0, 0);
testPreparedStatementsResetData();
// 2nd execute -> prepare + execute.
assertTestPreparedStatementsResult(testModify1.execute(), 4, testCol1.getName(), 10, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 1, testModify1, 1, 1);
// Fails preparing, execute as non-prepared.
assertTestPreparedStatementsResult(testModify2.execute(), 4, testCol2.getName(), 10, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 1, testModify2, 0, -1);
// Failed prepare also counts.
assertPreparedStatementsStatusCounts(testSession, 2, 1, 0);
testPreparedStatementsResetData();
// 3rd execute -> execute.
assertTestPreparedStatementsResult(testModify1.execute(), 4, testCol1.getName(), 10, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 1, testModify1, 1, 2);
// Execute as non-prepared.
assertTestPreparedStatementsResult(testModify2.execute(), 4, testCol2.getName(), 10, 20, 30, 40);
assertPreparedStatementsCountsAndId(testSession, 1, testModify2, 0, -1);
assertPreparedStatementsStatusCounts(testSession, 2, 2, 0);
testPreparedStatementsResetData();
testSession.close();
// Prepared statements won't live past the closing of the session.
assertPreparedStatementsCount(sessionThreadId, 0, 10);
} finally {
this.session.sql("SET GLOBAL max_prepared_stmt_count = ?").bind(origMaxPrepStmtCount).execute();
}
} finally {
for (int i = 0; i < 4; i++) {
dropCollection(this.collectionName + "_" + (i + 1));
}
}
}
use of com.mysql.cj.xdevapi.SessionFactory in project aws-mysql-jdbc by awslabs.
the class AsyncQueryTest method overlappedAsyncQueries.
@Test
public void overlappedAsyncQueries() throws Exception {
final int NUMBER_OF_QUERIES = 1000;
Session sess = null;
try {
sess = new SessionFactory().getSession(this.baseUrl);
Collection coll = sess.getSchema(this.schema.getName()).getCollection(this.collection.getName());
String json1 = "{'mode': 'sync'}".replaceAll("'", "\"");
String json2 = "{'mode': 'async'}".replaceAll("'", "\"");
if (!mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
// Inject an _id.
json1 = json1.replace("{", "{\"_id\": \"1\", ");
json2 = json2.replace("{", "{\"_id\": \"2\", ");
}
AddResult res = coll.add(json1).add(json2).execute();
if (mysqlVersionMeetsMinimum(ServerVersion.parseVersion("8.0.5"))) {
assertTrue(res.getGeneratedIds().get(0).matches("[a-f0-9]{28}"));
assertTrue(res.getGeneratedIds().get(1).matches("[a-f0-9]{28}"));
} else {
assertEquals(0, res.getGeneratedIds().size());
}
List<CompletableFuture<DocResult>> futures = new ArrayList<>();
for (int i = 0; i < NUMBER_OF_QUERIES; ++i) {
if (i % 5 == 0) {
futures.add(CompletableFuture.completedFuture(coll.find("mode = 'sync'").execute()));
} else {
futures.add(coll.find("mode = 'async'").executeAsync());
}
}
for (int i = 0; i < NUMBER_OF_QUERIES; ++i) {
try {
DocResult docs = futures.get(i).get();
DbDoc d = docs.next();
JsonString mode = (JsonString) d.get("mode");
if (i % 5 == 0) {
assertEquals("sync", mode.getString(), "i = " + i);
} else {
assertEquals("async", mode.getString(), "i = " + i);
}
} catch (Throwable t) {
throw new Exception("Error on i = " + i, t);
}
}
} finally {
if (sess != null) {
sess.close();
sess = null;
}
}
}
Aggregations