MariaDB Connector J : autoReconnect does not wok for Basic Faillover - jdbc

From https://mariadb.com/kb/en/library/about-mariadb-connector-j/, for option autoReconnect, When this parameter enabled when a Failover and Load Balancing Mode is not in use, the connector will simply try to reconnect to its host after a failure. This is referred to as Basic Failover.
But the problem is that the reconnect does not work after server failure. The test code is as follows:
#Test
public void waitTimeoutResultSetTest() throws SQLException, InterruptedException {
try (Connection connection = setBlankConnection("&autoReconnect=true")) {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1");
assertTrue(rs.next());
stmt.execute("set session wait_timeout=1");
Thread.sleep(3000); // Wait for the server to kill the connection
try {
rs = stmt.executeQuery("show databases;");
assertTrue(rs.next());
System.out.println("position 1");
} catch (SQLException e) {
//normal exception
System.out.println("position 2");
}
}
}
With autoReconnect, I think the expected result is that it will get to position 1, but actually it will get to position 2, with exception that "Connection reset by peer: socket write error."
My question is that whether the basic failover does not work, or my test code is wrong? I cannot find other information from web, could you kindly give me some explanation if you know about it?

Related

Oracle MAF Error "Unable to read DataControl Usages, on loadDataControl for id:"

I have created a simple SOAP web service for a remote DB which is working fine in WebLogic 12c server and also in Jdeveloper HTTP analyzer. When I am using the same web service in the code below, I am getting the following error (I have included the System.out.println outputs in ref. to the flow of the code) I am using Jdeveloper 12C MAF 2.0.0.0.41 on Mac OSx 10.9.
Chk #0
Processing row# 1
Chk #1
Chk #2
[SEVERE - oracle.adfmf.framework - AmxBindingContext - loadDataControlById] Unable to read DataControl Usages, on loadDataControl for id: WLFNewActWS.
[SEVERE - oracle.adfmf.framework - SynchronizationDC - syncDataFromOfflineToOnline] [Ljava.lang.StackTraceElement;#467c53d3
public void syncDataFromOfflineToOnline() {
Trace.log(Utility.FrameworkLogger, Level.INFO, this.getClass(), "syncDataFromOfflineToOnline",
"Executing syncDataFromOfflineToOnline Method");
try {
Connection conn = DBConnectionFactory.getConnection();
conn.setAutoCommit(false);
String select = "SELECT * FROM DEPARTMENTS";
PreparedStatement pStmt = conn.prepareStatement(select);
ResultSet rs = pStmt.executeQuery();
System.out.println("Chk #0");
int rowcount = 0;
while (rs.next()) {
rowcount++;
System.out.println("Processing row# " + rowcount);
List namesList = new ArrayList(1);
List paramsList = new ArrayList(1);
List typesList = new ArrayList(1);
Department dept = new Department();
dept.setDeptId(rs.getInt("DEPARTMENT_ID"));
dept.setDeptName(rs.getString("DEPARTMENT_NAME"));
dept.setMgrId(rs.getInt("MANAGER_ID"));
dept.setLocId(rs.getInt("LOCATION_ID"));
System.out.println("Chk #1");
GenericType gtDept =
GenericTypeBeanSerializationHelper.toGenericType("WLFNewActWS.Types.persistDepartments.arg0", dept);
System.out.println("Chk #2");
namesList.add("arg0");
paramsList.add(gtDept);
typesList.add(GenericType.class);
AdfmfJavaUtilities.invokeDataControlMethod("WLFNewActWS", null, "persistDepartments", namesList,
paramsList, typesList);
System.out.println("Chk #3");
}
} catch (SQLException e) {
Trace.log(Utility.FrameworkLogger, Level.SEVERE, this.getClass(), "syncDataFromOfflineToOnline",
e.getMessage());
} catch (Exception e) {
Trace.log(Utility.FrameworkLogger, Level.SEVERE, this.getClass(), "syncDataFromOfflineToOnline",
e.getStackTrace());
}
}
This may not be the answer to this question, but it may be the answer for someone searching for "Unable to read DataControl Usages, on loadDataControl for id"
In my case it is with Oracle MAF, but this also applies to Oracle ADF.
Your web service needs to be in your DataBindings.cpx file.
Something like:
<dataControlUsages>
<dc id="WLFNewActWS " path="mobile.WLFNewActWS "/>
</dataControlUsages>
I cheat and drag something from the WS into a one of pages and have jdeveloper add the record in DataBindings.cpx for me.
This occurs of the binding issue. Please create a reference/binding to 'WLFNewActWS' in your amx page. It will solve your problem...!

kerberos auth and connection pooling in jdbc

I've got Java web application running on Tomcat with SSO via SPNEGO/Kerberos and I want to pass kerberos ticket to database, Oracle DB in my case (like impersonation in MS products). I've found an example of implementation (http://docs.oracle.com/cd/B28359_01/java.111/b31224/clntsec.htm):
Connection conn = (Connection)Subject.doAs(specificSubject, new PrivilegedExceptionAction({
public Object run() {
Connection con = null;
Properties prop = new Properties();
prop.setProperty(AnoServices.AUTHENTICATION_PROPERTY_SERVICES,"("+AnoServices.AUTHENTICATION_KERBEROS5 + ")");
try {
OracleDriver driver = new OracleDriver();
con = driver.connect(url, prop);
}catch (Exception except){
except.printStackTrace();
}
return con;
}
});
String auth = ((OracleConnection)conn).getAuthenticationAdaptorName();
System.out.println("Authentication adaptor="+auth);
printUserName(conn);
conn.close();
But as it is known to create a new connection is an expensive operation. To solve this problem commonly used connection pooling (like c3p0), but I cant find example, how to combine code above and connection pool. Is there any example?

How to close refcursor in spring when using simplejdbccall

I am using spring simpleJdbcCall to call oracle stored procedure and i am using oracle 11g .
I stumbled on a couple of posts which suggests there might be memory leak as the ref cursors are not properly closed by spring.
Is there anyway to explicitly close cursor while using spring simplejdbccall? or is increasing the oracle OPEN_CURSOR the only way out?.
I am planning to scale up my application to handle around one million transactions every hour .Any suggestions will be helpful.
Actually there is no such an issue with Spring JDBC. It closes all resources within finally after all execute. SimpleJdbcCall uses JdbcTemplate:
public <T> T execute(CallableStatementCreator csc, CallableStatementCallback<T> action)
throws DataAccessException {
try {
...
}
catch (SQLException ex) {
...
}
finally {
if (csc instanceof ParameterDisposer) {
((ParameterDisposer) csc).cleanupParameters();
}
JdbcUtils.closeStatement(cs);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
The same for ResultSet OUT parameters:
protected Map<String, Object> processResultSet(ResultSet rs, ResultSetSupportingSqlParameter param) throws SQLException {
....
finally {
JdbcUtils.closeResultSet(rs);
}
return returnedResults;
}
From other side I have a big experience with Spring JDBC and Oracle in high-loaded systems and want to say that we noticed enough open resources on Oracle with at peak loads, but they have been released properly after that.
Although we used JBOSS Pooled DataSource and its TransactionMaanger
I use CallableStatement directly and I can release statements and connections quickly and safely, try both methods and measure memory consumption, it worked perfectly for me to solve memory consumption and connection retention problems that proved many waiting and rejection of connections the applications.
try {
log.info("**** RepositoryPSostgres.getAllProducts ******** ");
Connection conn = jdbcTemplate.getDataSource().getConnection();
conn.setAutoCommit(false);
// Procedure call.
CallableStatement proc = conn.prepareCall("{? = call get_all_products() }");
proc.registerOutParameter(1, Types.OTHER);
proc.execute();
ResultSet results = (ResultSet) proc.getObject(1);
**proc.close();
proc.isClosed();
conn.close();**
ArrayList <Products> resp = new ArrayList <Products>();
while (results.next()) {
Products resp1 = new Products();
resp1.setId(results.getInt("id"));
resp1.setName((String) results.getString("name"));
resp1.setPrice((BigDecimal) results.getBigDecimal("price"));
resp.add(resp1);
log.info("***" + results.getInt("id") + "***** ");
log.info("***" + results.getString("name") + "***** ");
log.info("***" + results.getBigDecimal("price") + "***** ");
}
results.close();
return resp;
} catch (Exception e) {
e.printStackTrace();
log.error(new StringBuffer("Error en transaccion en saldo CashPooling : ").append(e.getLocalizedMessage()).toString());
return null;
}

difference between connection.close() and connection= null

If I explicitly close the connection by calling close() on connection object, i have set connection object to null. What is difference in close() and null on connection object?
If i close connection ,still connection object maintained in connection pool?
for e.g.
Connection dbConnection=null;
PreparedStatement preparedStatement = null;
ResultSet rs;
try {
Connection dbConnection= DriverManager.getConnection("jdbc:hsqldb:file:test5","sa", "");
...........
...........
dbConnection.close();
dbConnection=null;
} catch (Exception e) {
LOGGER.error("Exception Occured while fetching All record:Item details start method "
+ e.getMessage());
} finally {
try
{
if (rs!=null)
{
rs.close();
rs=null;
}
}
catch(SQLException e)
{
LOGGER.error(RESULTSETCLOSEEXCEPTION
+ e.getMessage());
}
try {
if (preparedStatement != null) {
preparedStatement.close();
preparedStatement=null;
}
} catch (SQLException e) {
LOGGER.error(STATEMENTCLOSEEXCEPTION
+ e.getMessage());
}
try {
if (dbConnection != null) {
dbConnection.close();
dbConnection=null;
}
} catch (SQLException e) {
LOGGER.error(CONNECTIONCLOSEEXCEPTION
+ e.getMessage());
}
}
Is above code is correct way to close connection, prepared statement and resultset?
From the documentation.
close()
Releases this Connection object's database and JDBC resources immediately instead of
waiting for them to be automatically released.
One closes the connection, one sets the connection reference to null.
If you don't close the connection, you can have a connection leak. It is important to close the connection in a finally block.
The close() operation closes the connection--it doesn't do anything to the connection reference. You might not be able to do anything with the connection, but it's not null. Once it's closed it can be released back into a collection pool, but that's something different yet again.
Conclusion::
*connection.close()*
it close the conection with the database and release all the resources.
***con = null*** - the reference to connection object is deleted in that case if the connection is open then it is still open i.e. resources are not free.
Let me correct if i am getting wrong.
by using Connection.close() we can close the resource and we can reuse the connection, because it is go back and stored into the connection pool
and
by making Connection connection = null it sense we are free up the connection resource, by this there is no leakage in the memory management, but we can't reuse it.

Hive JDBC getConnection does not return

I'm following the hive JDBC tutorial. I could not get it working. When it try to get the connection it just hangs. It does not report any error either. I'm sure the Hive server is running. Any help?
public class HiveJdbcClient {
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
public static void main(String[] args){
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
try{
Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
System.out.println("got the connection");
}catch(SQLException e){
e.printStackTrace();
}
}
}
output of the netstat:
$ sudo netstat -anlp | grep 10000
Password:
tcp 0 0 0.0.0.0:10000 0.0.0.0:* LISTEN 27738/java
tcp 107 0 127.0.0.1:10000 127.0.0.1:45910 ESTABLISHED 27738/java
tcp 0 0 127.0.0.1:33665 127.0.0.1:10000 ESTABLISHED 24475/java
tcp 0 0 127.0.0.1:45910 127.0.0.1:10000 ESTABLISHED 7445/java
tcp 107 0 127.0.0.1:10000 127.0.0.1:33665 ESTABLISHED 27738/java
Naresh: Try stopping the triffserver, then move to the HIVE_HOME/bin directory from your terminal, then start the hive trift server using the ./hive --service hiveserver 10000 & command. Then try running the program. Do a create table as per the hive client wiki example . Then do a show tables query in the next step. Let us know the result once this steps are followed. We can have a discussion after that.
You can do the following to pinpoint where the hang is happening. Here is an example that I did to trace it in my broken Hive JDBC connection. Note that this is not a concrete solution to any generic hive connection hanging error.
This is an answer to the question: "how can I find out where my JDBC hive connection is hanging? "
What makes this hard to trace is the JDBC dynamic invocation. Instead, you can just manually createa HiveConnection() class. That allows you to add some tracing into the code directly, to see where the hang is happening.
I've traced this by doing the following.
* USING LOG4J *
The thrift and other JDBC hive classes use log4j when connecting, if you turn DEBUG logging on, you can see fine grained errors. You can do this easily by adding
BasicConfigurator.configure()
To your client app. In any case, doing this led me to find that this seems to be stalling in the SASL transport layer. I guess it could be security related but I would assume that a security error would STILL return, and not hang... So I think this may be worthy of a JIRA. I've pasted a follow up question:
How can I trace the failure ot TSaslTransport (hive related)
* ANOTHER METHOD *
1) You can grab a copy of the "HiveConnection" class from github, or wherever, and instantiate a new one:
String url=String.format("jdbc:hive2://%s:%s/default",
server,
port)
Properties p = new Properties();
p.setProperty("host", con);
Connection jdbc = new HiveConnection(url,p);
Then, you can add your debugger hooks or log statements to the HiveConnection() class.
Ulitmately, when i had this error, I traced it to:
openTransport
Which ultimately creates a
org.apache.thrift.transport.TSaslClientTransport
instance.
And the hang happens in this code block:
try {
System.out.println(".....1 carrying on... attempting to open. " + transport.getClass().getName());
transport.open();
System.out.println("done open.");
}
catch (TTransportException e) {
System.out.println("2 fail .");
e.printStackTrace();
throw new SQLException("Could not establish connection to "
+ uri + ": " + e.getMessage(), " 08S01");
}
FYI I've posted a follow up regarding why MY connection failed. It might be related to yours as well... How can I trace the failure ot TSaslTransport (hive related)
I had the same problem/ Check this params:
driverName = "org.apache.hive.jdbc.HiveDriver"
con = DriverManager.getConnection("jdbc:hive2://192.168.1.93:10000/default", "", "");

Resources