difference between connection.close() and connection= null - jdbc

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.

Related

MariaDB Connector J : autoReconnect does not wok for Basic Faillover

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?

Configure Datasource with timezone in jboss/weblogic server

I am going to handle multiple time zone in my web application by resetting database session time zone every time getting DB connection.
The code looks like:
public Connection getConnection(GmDNSNamesEnum enDNSName) throws AppError
{
...
DataSource ds = (DataSource) ic.lookup(enDNSName.getDNSName());
conn = ds.getConnection();
conn.setAutoCommit(false);
setTimeZone(strTimeZone);
...
return conn;
}
private void setTimeZone(String strTimeZone){
Statement stmt = null;
if(conn != null) {
try{
stmt = conn.createStatement();
stmt.execute("alter session set time_zone=\'" + strTimeZone+"\'");
} catch (Exception e)
{
String strErrorMsg = GmCommonClass.getExceptionStackTrace(e);
log.error("Exception e "+strErrorMsg);
throw new AppError(e);
}
}
}
Is there any alternate way to set database session time zone?
now, I am looking for configure datasource with different timezone in jboss/weblogic server and use appropriate datasource specific to user timezone instead of every time reset the session time zone by executing alter session script.
thanks in advance
Wow ok.
Perhaps you could try persisting the dates and times into timezone-aware database columns instead of contorting the data source each time you connect? The convention is to store dates in UTC time, then do timezone conversion in the presentation layer (or at least post-retrieval).

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;
}

JMS QueueReceiver - Need to keep receiver wait for sometime even messages are available on Queue

I have to update my existing JMS Receiver program to as follows.
Existing Functionality:
My receiver class will read a message and calls a web service to process the job in one of the server once the message is received as xml.
New Functionality:
The receiver should wait for sometime until the job server is free to process a job. I tried using MessageSelectors but which is only applicable for message headers.I tried this option "message = (JMSTextMessage) mqQueueReceiver.receive(100000000000000);" but whenever i posted a message those message is read after posted into queue. But i want to keep receiver to wait for some interval which i am getting from Job server through web service call.
My Code is below:
connectionFactory = new MQQueueConnectionFactory();
connectionFactory.setHostName(config.getValue("host"));
connectionFactory.setPort(Integer.parseInt(config.getValue("port")));
connectionFactory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
connectionFactory.setQueueManager(config.getValue("manager"));
connectionFactory.setChannel(config.getValue("channel"));
queueConnection = (MQQueueConnection) connectionFactory.createQueueConnection();
queueSession = (MQQueueSession) queueConnection.createQueueSession(true, Session.CLIENT_ACKNOWLEDGE);
queue = (MQQueue) queueSession.createQueue(config.getValue("queue"));
mqQueueReceiver = (MQQueueReceiver) queueSession.createReceiver(queue);
while(true) {
if(this.stopListener) {
System.out.println("stopListener variable is changed ");
break;
}
try {
message = (JMSTextMessage) mqQueueReceiver.receive(1000);
String response = "";
if(this.nullCheckJMSTextObject(message)) {
response= soapClient.invokeWebService(message.getText(),message.getJMSCorrelationID());
if(this.nullCheckSoapResponse(response)) {
queueSession.commit();
} else {
queueSession.rollback();
queueSession.commit();
Thread.sleep(receiverWaitTime);
}
}
} catch (JMSException e) {
System.err.println("Linked Exception");
e.getLinkedException();
System.err.println("Error Code");
e.getErrorCode();
System.err.println("Cause ");
e.getCause();
System.err.println("fillTrackTrace ");
e.fillInStackTrace();
e.printStackTrace();
break;
}catch(IllegalStateException e) {
e.printStackTrace();
break;
}catch(InterruptedException e) {
e.printStackTrace();
break;
}catch(Exception e) {
e.printStackTrace();
break;
}
}
The receive(timeout) method will wait for the specified timeout period for a message to arrive on a queue. If a message arrives on a queue before the timeout, the method will return immediately with a message otherwise the method will wait till the timeout period and then return with no message. You will see a 2033 exception.
The timeout specified for the receive() call indicates how long the receive method must wait for messages before it can return. The timeout specified is not to delay the message delivery. If there is a message, the method will return immediately.
I think your logic can be modified to alter the order of execution. Change the code to receive messages only when your web service is ready to process messages.

Resources