use of org.h2.jdbc.JdbcConnection in project h2database by h2database.
the class TestPowerOff method testCrash.
private void testCrash() throws SQLException {
if (config.networked) {
return;
}
deleteDb(dir, DB_NAME);
Random random = new Random(1);
int repeat = getSize(1, 20);
for (int i = 0; i < repeat; i++) {
Connection conn = getConnection(url);
conn.close();
conn = getConnection(url);
Statement stat = conn.createStatement();
stat.execute("SET WRITE_DELAY 0");
((JdbcConnection) conn).setPowerOffCount(random.nextInt(100));
try {
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST" + "(ID INT PRIMARY KEY, NAME VARCHAR(255))");
conn.setAutoCommit(false);
int len = getSize(3, 100);
for (int j = 0; j < len; j++) {
stat.execute("INSERT INTO TEST VALUES(" + j + ", 'Hello')");
if (random.nextInt(5) == 0) {
conn.commit();
}
if (random.nextInt(10) == 0) {
stat.execute("DROP TABLE IF EXISTS TEST");
stat.execute("CREATE TABLE TEST" + "(ID INT PRIMARY KEY, NAME VARCHAR(255))");
}
}
stat.execute("DROP TABLE IF EXISTS TEST");
conn.close();
} catch (SQLException e) {
if (!e.getSQLState().equals("90098")) {
TestBase.logError("power", e);
}
}
}
}
use of org.h2.jdbc.JdbcConnection in project h2database by h2database.
the class TestPowerOff method testLobCrash.
private void testLobCrash() throws SQLException {
if (config.networked) {
return;
}
deleteDb(dir, DB_NAME);
Connection conn = getConnection(url);
Statement stat = conn.createStatement();
stat.execute("create table test(id identity, data clob)");
conn.close();
conn = getConnection(url);
stat = conn.createStatement();
stat.execute("set write_delay 0");
((JdbcConnection) conn).setPowerOffCount(Integer.MAX_VALUE);
stat.execute("insert into test values(null, space(11000))");
int max = Integer.MAX_VALUE - ((JdbcConnection) conn).getPowerOffCount();
for (int i = 0; i < max + 10; i++) {
conn.close();
conn = getConnection(url);
stat = conn.createStatement();
stat.execute("insert into test values(null, space(11000))");
stat.execute("set write_delay 0");
((JdbcConnection) conn).setPowerOffCount(i);
try {
stat.execute("insert into test values(null, space(11000))");
} catch (SQLException e) {
// ignore
}
JdbcUtils.closeSilently(conn);
}
}
use of org.h2.jdbc.JdbcConnection in project siena by mandubian.
the class FullText method parseKey.
/**
* Parse a primary key condition into the primary key columns.
*
* @param conn the database connection
* @param key the primary key condition as a string
* @return an array containing the column name list and the data list
*/
protected static Object[][] parseKey(Connection conn, String key) {
ArrayList<String> columns = New.arrayList();
ArrayList<String> data = New.arrayList();
JdbcConnection c = (JdbcConnection) conn;
Session session = (Session) c.getSession();
Parser p = new Parser(session);
Expression expr = p.parseExpression(key);
addColumnData(columns, data, expr);
Object[] col = new Object[columns.size()];
columns.toArray(col);
Object[] dat = new Object[columns.size()];
data.toArray(dat);
Object[][] columnData = { col, dat };
return columnData;
}
use of org.h2.jdbc.JdbcConnection in project ignite by apache.
the class ValidateIndexesClosure method mvccSession.
/**
* Get session with MVCC snapshot and QueryContext.
*
* @param cctx Cache context.
* @return Session with QueryContext and MVCC snapshot.
* @throws IgniteCheckedException If failed.
*/
private Session mvccSession(GridCacheContext<?, ?> cctx) throws IgniteCheckedException {
Session session = null;
boolean mvccEnabled = cctx.mvccEnabled();
if (mvccEnabled) {
ConnectionManager connMgr = ((IgniteH2Indexing) ignite.context().query().getIndexing()).connections();
JdbcConnection connection = (JdbcConnection) connMgr.connection().connection();
session = (Session) connection.getSession();
MvccQueryTracker tracker = MvccUtils.mvccTracker(cctx, true);
MvccSnapshot mvccSnapshot = tracker.snapshot();
final QueryContext qctx = new QueryContext(0, cacheName -> null, null, mvccSnapshot, null, true);
session.setVariable(H2Utils.QCTX_VARIABLE_NAME, new H2Utils.ValueRuntimeSimpleObject<>(qctx));
}
return session;
}
use of org.h2.jdbc.JdbcConnection in project frostwire by frostwire.
the class FullTextLucene2 method search.
/**
* Do the search.
*
* @param conn the database connection
* @param text the query
* @param limit the limit
* @param offset the offset
* @param data whether the raw data should be returned
* @return the result set
*/
protected static ResultSet search(Connection conn, String text, int limit, int offset, boolean data) throws SQLException {
SimpleResultSet result = createResultSet(data);
if (conn.getMetaData().getURL().startsWith("jdbc:columnlist:")) {
// this is just to query the result set columns
return result;
}
if (text == null || text.trim().length() == 0) {
return result;
}
try {
IndexAccess access = getIndexAccess(conn);
/*## LUCENE2 ##
access.modifier.flush();
String path = getIndexPath(conn);
IndexReader reader = IndexReader.open(path);
Analyzer analyzer = new StandardAnalyzer();
Searcher searcher = new IndexSearcher(reader);
QueryParser parser = new QueryParser(LUCENE_FIELD_DATA, analyzer);
Query query = parser.parse(text);
Hits hits = searcher.search(query);
int max = hits.length();
if (limit == 0) {
limit = max;
}
for (int i = 0; i < limit && i + offset < max; i++) {
Document doc = hits.doc(i + offset);
float score = hits.score(i + offset);
//*/
// ## LUCENE3 ##
// take a reference as the searcher may change
Searcher searcher = access.searcher;
// reuse the same analyzer; it's thread-safe;
// also allows subclasses to control the analyzer used.
Analyzer analyzer = access.writer.getAnalyzer();
QueryParser parser = new QueryParser(Version.LUCENE_30, LUCENE_FIELD_DATA, analyzer);
Query query = parser.parse(text);
// Lucene 3 insists on a hard limit and will not provide
// a total hits value. Take at least 100 which is
// an optimal limit for Lucene as any more
// will trigger writing results to disk.
int maxResults = (limit == 0 ? 100 : limit) + offset;
TopDocs docs = searcher.search(query, maxResults);
if (limit == 0) {
limit = docs.totalHits;
}
for (int i = 0, len = docs.scoreDocs.length; i < limit && i + offset < docs.totalHits && i + offset < len; i++) {
ScoreDoc sd = docs.scoreDocs[i + offset];
Document doc = searcher.doc(sd.doc);
float score = sd.score;
// */
String q = doc.get(LUCENE_FIELD_QUERY);
if (data) {
int idx = q.indexOf(" WHERE ");
JdbcConnection c = (JdbcConnection) conn;
Session session = (Session) c.getSession();
Parser p = new Parser(session);
String tab = q.substring(0, idx);
ExpressionColumn expr = (ExpressionColumn) p.parseExpression(tab);
String schemaName = expr.getOriginalTableAliasName();
String tableName = expr.getColumnName();
q = q.substring(idx + " WHERE ".length());
Object[][] columnData = parseKey(conn, q);
result.addRow(schemaName, tableName, columnData[0], columnData[1], score);
} else {
result.addRow(q, score);
}
}
/*## LUCENE2 ##
// TODO keep it open if possible
reader.close();
//*/
} catch (Exception e) {
throw convertException(e);
}
return result;
}
Aggregations