hi everyone,
I deployed my code in server it works fine till some time say 1 hour but after that it gives org.hibernate.exception.JDBCConnectionException: Cannot open connection Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientC onnectionException:
Too many connections
How to avoid this exception ??? any help ??
---------- error message is--------------------------
entering main
configuration is done
connection got
query executed
Coefficent 1 is :5
Coefficent 0 is :5
Device ID is : 201001000002
Sensor type is : O3
SO2,EC4-20-SO2
entering main
configuration is done
connection got
Hibernate exception occured
org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert( SQLStateConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.conver t(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.conver t(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnectio n(ConnectionManager.java:420)
at org.hibernate.jdbc.ConnectionManager.getConnection (ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCCont ext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JD BCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(Se ssionImpl.java:1326)
at FetchCoeff.testing(FetchCoeff.java:37)
at UDPServer.main(UDPServer.java:70)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientC onnectionException:
Too many connections
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Construc tor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:40 6)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:985)
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:3308)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:894)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java: 3823)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:12 56)
at com.mysql.jdbc.ConnectionImpl.createNewIO(Connecti onImpl.java:2032)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImp l.java:729)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connect ion.java:46)
at sun.reflect.GeneratedConstructorAccessor17.newInst ance(Unknown
Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Construc tor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:40 6)
at com.mysql.jdbc.ConnectionImpl.getInstance(Connecti onImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:283)
at java.sql.DriverManager.getConnection(DriverManager .java:620)
at java.sql.DriverManager.getConnection(DriverManager .java:169)
at org.hibernate.connection.DriverManagerConnectionPr ovider.getConnection(DriverManagerConnectionProvid er.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnectio n(ConnectionManager.java:417)
... 6 more
Exception in thread "main" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1009)
at java.lang.Float.parseFloat(Float.java:439)
at ConvertReadings.Convertd(ConvertReadings.java:52)
at UDPServer.main(UDPServer.java:72)
hibernate configuration file is
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/kiran
</property>
<property name="connection.username">
kiran
</property>
<property name="connection.password">
kiran123
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="show_sql">false</property>
<mapping resource="SensorReadings.hbm.xml"/>
</session-factory>
</hibernate-configuration>
the fetching code is
/**
*
*/
/**
*
*
*/
import java.util.Iterator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class FetchCoeff {
public String[] testing(String x, String y) { ///here x=DevID and y= sensor type
Session session= null ;
Transaction tx= null;
String c[] = new String[30];
System.out.println("entering main");
Logger.getRootLogger().setLevel(Level.OFF);
Configuration cfg= new Configuration();
cfg.configure("Hibernate.cfg.xml");
System.out.println("configuration is done");
session = cfg.buildSessionFactory().openSession();
System.out.println("connection got");
try{
tx = session.beginTransaction();
String q= "SELECT d.c1, d.c0, d.DevID, d.SensorType FROM SensorReadings d WHERE d.DevID= evID AND d.SensorType= :SensorType";
Query query = session.createQuery(q);
query.setParameter("DevID",x);
query.setParameter("SensorType", y);
System.out.println("query executed");
for(Iterator it=query.iterate();it.hasNext(){
Object[] ob = (Object[]) it.next();
System.out.println("Coefficent 1 is :" + ob[0]);
System.out.println("Coefficent 0 is :" + ob[1]);
System.out.println("Device ID is : " + ob[2]);
System.out.println("Sensor type is : " + ob[3]);
c[1]= (String)ob[0]; // contains coeff 1 c1
c[0]= (String)ob[1]; // contains coeff 0 c0
tx.commit();
}
}catch(HibernateException e){
System.out.println("Hibernate exception occured");
e.printStackTrace();
}catch(Exception d){
System.out.println("exception occured");
d.printStackTrace();
}
finally
{
session.close();
}
return c;
}
}
Can anyone help me in solving this problem ????