use of java.sql.ResultSet in project tomcat by apache.
the class JDBCStore method keys.
/**
* Return an array containing the session identifiers of all Sessions
* currently saved in this Store. If there are no such Sessions, a
* zero-length array is returned.
*
* @param expiredOnly flag, whether only keys of expired sessions should
* be returned
* @return array containing the list of session IDs
*
* @exception IOException if an input/output error occurred
*/
private String[] keys(boolean expiredOnly) throws IOException {
String[] keys = null;
synchronized (this) {
int numberOfTries = 2;
while (numberOfTries > 0) {
Connection _conn = getConnection();
if (_conn == null) {
return new String[0];
}
try {
String keysSql = "SELECT " + sessionIdCol + " FROM " + sessionTable + " WHERE " + sessionAppCol + " = ?";
if (expiredOnly) {
keysSql += " AND (" + sessionLastAccessedCol + " + " + sessionMaxInactiveCol + " * 1000 < ?)";
}
try (PreparedStatement preparedKeysSql = _conn.prepareStatement(keysSql)) {
preparedKeysSql.setString(1, getName());
if (expiredOnly) {
preparedKeysSql.setLong(2, System.currentTimeMillis());
}
try (ResultSet rst = preparedKeysSql.executeQuery()) {
ArrayList<String> tmpkeys = new ArrayList<>();
if (rst != null) {
while (rst.next()) {
tmpkeys.add(rst.getString(1));
}
}
keys = tmpkeys.toArray(new String[tmpkeys.size()]);
// Break out after the finally block
numberOfTries = 0;
}
}
} catch (SQLException e) {
manager.getContext().getLogger().error(sm.getString(getStoreName() + ".SQLException", e));
keys = new String[0];
// Close the connection so that it gets reopened next time
if (dbConnection != null)
close(dbConnection);
} finally {
release(_conn);
}
numberOfTries--;
}
}
return keys;
}
use of java.sql.ResultSet in project tomcat by apache.
the class JDBCStore method load.
/**
* Load the Session associated with the id <code>id</code>.
* If no such session is found <code>null</code> is returned.
*
* @param id a value of type <code>String</code>
* @return the stored <code>Session</code>
* @exception ClassNotFoundException if an error occurs
* @exception IOException if an input/output error occurred
*/
@Override
public Session load(String id) throws ClassNotFoundException, IOException {
StandardSession _session = null;
org.apache.catalina.Context context = getManager().getContext();
Log contextLog = context.getLogger();
synchronized (this) {
int numberOfTries = 2;
while (numberOfTries > 0) {
Connection _conn = getConnection();
if (_conn == null) {
return null;
}
ClassLoader oldThreadContextCL = context.bind(Globals.IS_SECURITY_ENABLED, null);
try {
if (preparedLoadSql == null) {
String loadSql = "SELECT " + sessionIdCol + ", " + sessionDataCol + " FROM " + sessionTable + " WHERE " + sessionIdCol + " = ? AND " + sessionAppCol + " = ?";
preparedLoadSql = _conn.prepareStatement(loadSql);
}
preparedLoadSql.setString(1, id);
preparedLoadSql.setString(2, getName());
try (ResultSet rst = preparedLoadSql.executeQuery()) {
if (rst.next()) {
try (ObjectInputStream ois = getObjectInputStream(rst.getBinaryStream(2))) {
if (contextLog.isDebugEnabled()) {
contextLog.debug(sm.getString(getStoreName() + ".loading", id, sessionTable));
}
_session = (StandardSession) manager.createEmptySession();
_session.readObjectData(ois);
_session.setManager(manager);
}
} else if (context.getLogger().isDebugEnabled()) {
contextLog.debug(getStoreName() + ": No persisted data object found");
}
// Break out after the finally block
numberOfTries = 0;
}
} catch (SQLException e) {
contextLog.error(sm.getString(getStoreName() + ".SQLException", e));
if (dbConnection != null)
close(dbConnection);
} finally {
context.unbind(Globals.IS_SECURITY_ENABLED, oldThreadContextCL);
release(_conn);
}
numberOfTries--;
}
}
return _session;
}
use of java.sql.ResultSet in project zeppelin by apache.
the class SqlCompleter method fillTableAndColumnNames.
/**
* Fill two map with list of tables and list of columns
*
* @param catalogName name of a catalog
* @param meta metadata from connection to database
* @param schemaFilter a schema name pattern; must match the schema name
* as it is stored in the database; "" retrieves those without a schema;
* <code>null</code> means that the schema name should not be used to narrow
* the search; supports '%' and '_' symbols; for example "prod_v_%"
* @param tables function fills this map, for every schema name adds
* set of table names within the schema
* @param columns function fills this map, for every table name adds set
* of columns within the table; table name is in format schema_name.table_name
*/
private static void fillTableAndColumnNames(String catalogName, DatabaseMetaData meta, String schemaFilter, Map<String, Set<String>> tables, Map<String, Set<String>> columns) {
try {
ResultSet cols = meta.getColumns(catalogName, schemaFilter, "%", "%");
try {
while (cols.next()) {
String schema = cols.getString("TABLE_SCHEM");
if (schema == null)
schema = cols.getString("TABLE_CAT");
String table = cols.getString("TABLE_NAME");
String column = cols.getString("COLUMN_NAME");
if (!isBlank(table)) {
String schemaTable = schema + "." + table;
if (!columns.containsKey(schemaTable))
columns.put(schemaTable, new HashSet<String>());
columns.get(schemaTable).add(column);
if (!tables.containsKey(schema))
tables.put(schema, new HashSet<String>());
tables.get(schema).add(table);
}
}
} finally {
cols.close();
}
} catch (Throwable t) {
logger.error("Failed to retrieve the column name", t);
}
}
use of java.sql.ResultSet in project hive by apache.
the class TestJdbcWithMiniLlap method createTestTable.
private void createTestTable(String tableName) throws Exception {
Statement stmt = hs2Conn.createStatement();
// create table
stmt.execute("DROP TABLE IF EXISTS " + tableName);
stmt.execute("CREATE TABLE " + tableName + " (under_col INT COMMENT 'the under column', value STRING) COMMENT ' test table'");
// load data
stmt.execute("load data local inpath '" + kvDataFilePath.toString() + "' into table " + tableName);
ResultSet res = stmt.executeQuery("SELECT * FROM " + tableName);
assertTrue(res.next());
assertEquals("val_238", res.getString(2));
res.close();
stmt.close();
}
use of java.sql.ResultSet in project hive by apache.
the class TestIncrementalRowsWithNormalization method testIncrementalRows.
@Test
public void testIncrementalRows() throws SQLException {
Integer incrementalBufferRows = 5;
// Mock BeeLineOpts
BeeLineOpts mockBeeLineOpts = mock(BeeLineOpts.class);
when(mockBeeLineOpts.getIncrementalBufferRows()).thenReturn(incrementalBufferRows);
when(mockBeeLineOpts.getMaxColumnWidth()).thenReturn(BeeLineOpts.DEFAULT_MAX_COLUMN_WIDTH);
when(mockBeeLineOpts.getNumberFormat()).thenReturn("default");
when(mockBeeLineOpts.getNullString()).thenReturn("NULL");
// Mock BeeLine
BeeLine mockBeeline = mock(BeeLine.class);
when(mockBeeline.getOpts()).thenReturn(mockBeeLineOpts);
// MockResultSet
ResultSet mockResultSet = mock(ResultSet.class);
ResultSetMetaData mockResultSetMetaData = mock(ResultSetMetaData.class);
when(mockResultSetMetaData.getColumnCount()).thenReturn(1);
when(mockResultSetMetaData.getColumnLabel(1)).thenReturn("Mock Table");
when(mockResultSet.getMetaData()).thenReturn(mockResultSetMetaData);
// First 10 calls to resultSet.next() should return true
when(mockResultSet.next()).thenAnswer(new Answer<Boolean>() {
private int iterations = 10;
@Override
public Boolean answer(InvocationOnMock invocation) {
return this.iterations-- > 0;
}
});
when(mockResultSet.getString(1)).thenReturn("Hello World");
// IncrementalRows constructor should buffer the first "incrementalBufferRows" rows
IncrementalRowsWithNormalization incrementalRowsWithNormalization = new IncrementalRowsWithNormalization(mockBeeline, mockResultSet);
// When the first buffer is loaded ResultSet.next() should be called "incrementalBufferRows" times
verify(mockResultSet, times(5)).next();
// Iterating through the buffer should not cause the next buffer to be fetched
for (int i = 0; i < incrementalBufferRows + 1; i++) {
incrementalRowsWithNormalization.next();
}
verify(mockResultSet, times(5)).next();
// When a new buffer is fetched ResultSet.next() should be called "incrementalBufferRows" more times
incrementalRowsWithNormalization.next();
verify(mockResultSet, times(10)).next();
}
Aggregations