I'm a bit lost about this fact:
show status like 'con%';
+-----------------------------------+-------+
| Variable_name | Value |
+-----------------------------------+-------+
| Connection_errors_accept | 0 |
| Connection_errors_internal | 0 |
| Connection_errors_max_connections | 0 |
| Connection_errors_peer_address | 0 |
| Connection_errors_select | 0 |
| Connection_errors_tcpwrap | 0 |
| Connections | 10535 |
+-----------------------------------+-------+
I read some similar question here, but the problems in those cases where not mine, so here I am.
I use MySQL and Hibernate. In my webapp there is this static HibernateUtil class to access the database:
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final Logger log = Logger.getLogger(HibernateUtil.class);
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
// error handling
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = null;
try {
s = (Session) session.get();
} catch(Exception e) {
// error handling
}
// Open a new Session, if this thread has none yet
if (s == null) {
try {
s = sessionFactory.openSession();
} catch(Exception e) {
// error handling
}
try {
s.getTransaction();
} catch(Exception e){
// error handling
}
Transaction tx = null;
while(tx==null){
try {
tx = s.beginTransaction();
// Store it in the ThreadLocal variable
} catch(Exception j) {
// error handling
}
}
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
if (s != null){
try {
s.getTransaction().commit();
s.close();
} catch(Exception e) {
// error handling
}
}
session.set(null);
}
public static void errorSession() throws HibernateException {
Session s = (Session) session.get();
try {
s.getTransaction().rollback();
s.close();
} catch(Exception e) {
// error handling
}
session.set(null);
}
}
Then I call the util class like in this example:
private MyTable getMyTable() {
try {
Session session = currentSession();
// some prepared statement
return myTable;
} catch (HibernateException e) {
errorSession();
return null;
} finally {
closeSession();
}
}
So basically I close the connection on success (closeSession) and on error (errorSession). Now why do I see so many connections in the MySQL console?
The meaning of connections
is not what you think.
As said in docs connections
means:
The number of connection attempts (successful or not) to the MySQL server.
So you do not have 10535 active connections as you think.
To see the actual connect threads use:
SHOW STATUS LIKE 'Threads_connected';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With