Will DB connection leakage cause too many inactive session in Oracle? - java

I was assigned a task about fixing too many inactive session in Oracle database which used in our Java application. We found there was java method which didn't close the JDBC connetion. I know this is a DB connection leakage, but I am not sure if it is the cause of the too many inactive session. I don't know if O can get to know which Java process cause this issue. Can someone help me?

I know this is a DB Connection leakage but I am not sure if it is the reason cause the too many too many inactive session.
Probably.
If each time you get a JDBC Connection you do actually create a new connection then you will also start a new session and when you do not close the connection then you will have an inactive session and the number of sessions will grow.
If you are using connection pooling then, when you close the connection, the connection is not actually closed but is returned to the pool. When the next connection is required it will request a connection from the pool and you will reuse the previous connection and the previous connection's session. In this case you should not see an increase in the total number of sessions while the connections are reused from the connection pool but you might see inactive sessions that are the pooled connections which are not currently in use.
It sounds like you are not using connection pooling and then the number of sessions will directly correlate to the number of connections.

Related

Will connection pool keep oracle session staying active?

i have a question about connection pool and oracle database. if i set the min size of the connection pool to 5, does that means even when the app is idle, oracle database will still keep 5 sessions active? thanks!
Yes, it should keep connections even if app is idle. Good idea is to check if connection on pool is still alive. Wrappers like HikariCP/Commons DBCP handle such cases.
//edit
Connection pool wont start 5 connections on application start but it will initialize new connection if it will be needed. And of course pool can be set to shutdown connection after finishing operation on it.
Connection pool behavior doc link

Large amount of Inactive sessions in Oracle by Java

I have a web application which needs to connect to the database every now and then, I am sure that I am closing every instance of new connection that I am opening.
The issue is I have a lot of inactive sessions in oracle db of same user. I have tried pooling, I have tried to close all sessions but nothing seems to work fine. I have searched for possible solutions over Stack Overflow but unfortunately did not find answer to my solution. The closest I get to is Inactive session in Oracle by JDBC where the person asking the question has itself answer the question by saying that he modified the code.
Any answer, recommendation would be appreciated
I have tried pooling...
Without a connection pool your application has a direct controll about opening and closing the database connections. This is not the typical case as the acquiring of a physical connection is a costly operation.
A connection pool optimizes it while keeping a particular number of connection open and provides them on request to the application.
If a connection is closed by application, it is not closed in the DB, it is made available in the pool as idle. You can control among other parameters how many idle connections should be kept in the pool. E.g. for DBCP check the parameters minIdle and maxIdle. Except for some special cases with invalid connections, the number of idle connection (those connenction are INACTIVE) you see should be within this limit.
If you see a systematic a higher number (or even an increasing number) of INACTIVE session, the most probable explanation is that the application gets the connection from the pool and "forget" to return it - those session are INACTIVE as well.

How are JDBC connections implemented?

I have a project in which I used HikariCP for JDBC connection pooling. And HikariCP works just great for my needs. It also logs the stats of the pool like below.
2014-12-03 10:16:08 DEBUG HikariPool:559 - Before cleanup pool stats loginPool (total=8, inUse=0, avail=8, waiting=0)
2014-12-03 10:16:08 DEBUG HikariPool:559 - After cleanup pool stats loginPool (total=7, inUse=1, avail=7, waiting=0)
Just for experimental purposes I closed all the MySQL connections for the configured database using MySQL Workbench. But, still I see HikariCP logging the stats like before though there are no actual connections to the database. When there was a request for connection it immediately established the connections(initial 8), so everything works great.
So, my question is how does these connections are managed or implemented? I think the reason why HikariCP logs stats, as if there were connections, is because it has valid in memory references to connections, which are actually non existent(with database).
Is my understanding correct?
When you close the connections using MySQL Workbench, you are closing them on the server end. On the JDBC (client) side, the previously established connections will remain in existence until the client code attempts to use them. At that point, they will be found to be "broken"; i.e. the client will get exceptions when it tries to use them.
The client-side JDBC Connection objects only get closed or recycled when they are returned to the connection pool by your Java application code.
The connection pool created 8 connections at startup. You say you disconnected them using the workbench. Most connection pools won't know the connection is disconnected until it gets used.
Your assumption is correct. You manually killed the connections but the pool has a handle to 8 sockets which it assumes are connected. Given time your connection pool may have checked the connections for validity and attempted to reconnect them. I can't speak for HikariCP but this is what modern connections pools do.

Tomcat JDBC Connection Pool - Connections stuck on active

My application uses Tomcat JDBC connection pool, with MySQL DB.
Seems like a process that run during the night (anti virus scan?) cause the memory and CPU on the machine to increase, and as a result connections from the pool stuck on active until the connection pool can't response to any connection request.
I'm getting errors like:
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.
Timeout: Pool empty. Unable to fetch a connection in 10 seconds, none available[size:100; busy:97; idle:0; lastwait:10000]. (That's weird, where are the remaining 3?)
Looking at a chart I'm generating describing the active connection state, it is flat until at some point it start increasing until it reach the maximum and stays there.
My connection pool is configure to remove unclosed connections (setRemoveAbandoned = true).
Do you have any idea how can I solve this issue?
I think this is because your application not closing connections after use. Please check your code and make sure all connections are closing after use.

Configure Hibernate to obtain a fresh connection from a connection pool

How do I configure Hibernate so that each time I call sessionFactory.openSession() it connects with a new connection from the connection pool? The connection pool is managed by Websphere Application Server and is a JDBC Data Source.
Thanks
How do I configure Hibernate so that each time I call sessionFactory.openSession() it connects with a new connection from the connection pool?
This is the default behavior, each session will get a dedicated connection from the connection pool.
Right now, it appears that both sessions are using the same connection, because when the first session is closed (manually calling session.close()) sometimes, the other session will throw a "session closed" exception when trying to run more queries on it.
No they are not. But maybe the second connection gets released at the end of the transaction initiated for the request. Have a look at the hibernate.connection.release_mode configuration parameter, you might want to use on_close. But without more details on your transaction strategy, it's impossible to say anything.
The second session is open by a child thread which means that the child thread can keep living even after the (HTTP) request is complete.
Take my previous advice with a grain of salt, you should just not spawn unmanaged threads and I don't know how the application server will behave. I explain in this other answer what would be the right way.

Categories