use of java.sql.Statement in project hive by apache.
the class CompactionTxnHandler method cleanEmptyAbortedTxns.
/**
* Clean up aborted transactions from txns that have no components in txn_components. The reason such
* txns exist can be that now work was done in this txn (e.g. Streaming opened TransactionBatch and
* abandoned it w/o doing any work) or due to {@link #markCleaned(CompactionInfo)} being called.
*/
@Override
@RetrySemantics.SafeToRetry
public void cleanEmptyAbortedTxns() throws MetaException {
try {
Connection dbConn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//Aborted is a terminal state, so nothing about the txn can change
//after that, so READ COMMITTED is sufficient.
dbConn = getDbConn(Connection.TRANSACTION_READ_COMMITTED);
stmt = dbConn.createStatement();
String s = "select txn_id from TXNS where " + "txn_id not in (select tc_txnid from TXN_COMPONENTS) and " + "txn_state = '" + TXN_ABORTED + "'";
LOG.debug("Going to execute query <" + s + ">");
rs = stmt.executeQuery(s);
List<Long> txnids = new ArrayList<>();
while (rs.next()) txnids.add(rs.getLong(1));
close(rs);
if (txnids.size() <= 0) {
return;
}
//easier to read logs
Collections.sort(txnids);
List<String> queries = new ArrayList<String>();
StringBuilder prefix = new StringBuilder();
StringBuilder suffix = new StringBuilder();
prefix.append("delete from TXNS where ");
suffix.append("");
TxnUtils.buildQueryWithINClause(conf, queries, prefix, suffix, txnids, "txn_id", false, false);
for (String query : queries) {
LOG.debug("Going to execute update <" + query + ">");
int rc = stmt.executeUpdate(query);
LOG.info("Removed " + rc + " empty Aborted transactions from TXNS");
}
LOG.info("Aborted transactions removed from TXNS: " + txnids);
LOG.debug("Going to commit");
dbConn.commit();
} catch (SQLException e) {
LOG.error("Unable to delete from txns table " + e.getMessage());
LOG.debug("Going to rollback");
rollbackDBConn(dbConn);
checkRetryable(dbConn, e, "cleanEmptyAbortedTxns");
throw new MetaException("Unable to connect to transaction database " + StringUtils.stringifyException(e));
} finally {
close(rs, stmt, dbConn);
}
} catch (RetryException e) {
cleanEmptyAbortedTxns();
}
}
use of java.sql.Statement in project hive by apache.
the class TestTxnUtils method runAgainstDerby.
/** Verify queries can run against Derby DB.
* As long as Derby doesn't complain, we assume the query is syntactically/semantically correct.
*/
private void runAgainstDerby(List<String> queries) throws Exception {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = TxnDbUtil.getConnection();
stmt = conn.createStatement();
for (String query : queries) {
rs = stmt.executeQuery(query);
Assert.assertTrue("The query is not valid", rs.next());
}
} finally {
TxnDbUtil.closeResources(conn, stmt, rs);
}
}
use of java.sql.Statement in project hive by apache.
the class ThriftCliServiceMessageSizeTest method testMessageSize.
@Test
public void testMessageSize() throws Exception {
String transportMode = "binary";
hiveConf.setBoolVar(ConfVars.HIVE_SERVER2_ENABLE_DOAS, false);
hiveConf.setVar(ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST, host);
hiveConf.setIntVar(ConfVars.HIVE_SERVER2_THRIFT_PORT, port);
hiveConf.setVar(ConfVars.HIVE_SERVER2_AUTHENTICATION, AuthTypes.NONE.toString());
hiveConf.setVar(ConfVars.HIVE_SERVER2_TRANSPORT_MODE, transportMode);
HiveServer2 hiveServer2 = new HiveServer2();
String url = "jdbc:hive2://localhost:" + port + "/default";
Class.forName("org.apache.hive.jdbc.HiveDriver");
try {
// First start HS2 with high message size limit. This should allow connections
hiveConf.setIntVar(ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE, 100 * 1024 * 1024);
startHiveServer2WithConf(hiveServer2, hiveConf);
System.out.println("Started Thrift CLI service with message size limit " + hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE));
// With the high message size limit this connection should work
Connection connection = DriverManager.getConnection(url, "hiveuser", "hive");
Statement stmt = connection.createStatement();
assertNotNull("Statement is null", stmt);
stmt.execute("set hive.support.concurrency = false");
connection.close();
stopHiveServer2(hiveServer2);
// Now start HS2 with low message size limit. This should prevent any connections
hiveConf.setIntVar(ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE, 1);
hiveServer2 = new HiveServer2();
startHiveServer2WithConf(hiveServer2, hiveConf);
System.out.println("Started Thrift CLI service with message size limit " + hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE));
Exception caughtException = null;
try {
// This should fail
connection = DriverManager.getConnection(url, "hiveuser", "hive");
} catch (Exception err) {
caughtException = err;
}
// Verify we hit an error while connecting
assertNotNull(caughtException);
} finally {
stopHiveServer2(hiveServer2);
hiveServer2 = null;
}
}
use of java.sql.Statement in project hive by apache.
the class TestHS2ImpersonationWithRemoteMS method testImpersonation.
@Test
public void testImpersonation() throws Exception {
assertTrue("Test setup failed. MiniHS2 is not initialized", miniHS2 != null && miniHS2.isStarted());
Class.forName(MiniHS2.getJdbcDriverName());
// Create two tables one as user "foo" and other as user "bar"
Connection hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL(), "foo", null);
Statement stmt = hs2Conn.createStatement();
String tableName = "foo_table";
stmt.execute("drop table if exists " + tableName);
stmt.execute("create table " + tableName + " (value string)");
stmt.close();
hs2Conn.close();
hs2Conn = DriverManager.getConnection(miniHS2.getJdbcURL(), "bar", null);
stmt = hs2Conn.createStatement();
tableName = "bar_table";
stmt.execute("drop table if exists " + tableName);
stmt.execute("create table " + tableName + " (value string)");
stmt.close();
hs2Conn.close();
MiniDFSShim dfs = miniHS2.getDfs();
FileSystem fs = dfs.getFileSystem();
FileStatus[] files = fs.listStatus(miniHS2.getWareHouseDir());
boolean fooTableValidated = false;
boolean barTableValidated = false;
for (FileStatus file : files) {
final String name = file.getPath().getName();
final String owner = file.getOwner();
if (name.equals("foo_table")) {
fooTableValidated = owner.equals("foo");
assertTrue(String.format("User 'foo' table has wrong ownership '%s'", owner), fooTableValidated);
} else if (name.equals("bar_table")) {
barTableValidated = owner.equals("bar");
assertTrue(String.format("User 'bar' table has wrong ownership '%s'", owner), barTableValidated);
} else {
fail(String.format("Unexpected table directory '%s' in warehouse", name));
}
System.out.println(String.format("File: %s, Owner: %s", name, owner));
}
assertTrue("User 'foo' table not found in warehouse", fooTableValidated);
assertTrue("User 'bar' table not found in warehouse", barTableValidated);
}
use of java.sql.Statement in project hive by apache.
the class cbo_rp_TestJdbcDriver2 method testErrorDiag.
@Test
public void testErrorDiag() throws SQLException {
Statement stmt = con.createStatement();
// verify syntax error
try {
stmt.executeQuery("select from " + dataTypeTableName);
fail("SQLException is expected");
} catch (SQLException e) {
assertEquals("42000", e.getSQLState());
}
// verify table not fuond error
try {
stmt.executeQuery("select * from nonTable");
fail("SQLException is expected");
} catch (SQLException e) {
assertEquals("42S02", e.getSQLState());
}
// verify invalid column error
try {
stmt.executeQuery("select zzzz from " + dataTypeTableName);
fail("SQLException is expected");
} catch (SQLException e) {
assertEquals("42000", e.getSQLState());
}
}
Aggregations