Win a copy of Rust Web Development this week in the Other Languages forum!

kiran gk

Greenhorn
+ Follow
since May 09, 2012
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by kiran gk

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.MySQLNonTransientConnectionException:
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.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at FetchCoeff.testing(FetchCoeff.java:37)
at UDPServer.main(UDPServer.java:70)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Too many connections
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:985)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:894)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3823)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1256)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2032)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.GeneratedConstructorAccessor17.newInstance(Unknown
Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
at java.sql.DriverManager.getConnection(DriverManager.java:620)
at java.sql.DriverManager.getConnection(DriverManager.java:169)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
... 6 more
Exception in thread "main" java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.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 ???
hi everyone
i have an issue, its an coding problem and i know its an silly mistake but i am new to this i am not able to solve so please any one help me in solving this,

I made a query to the DB and got an object which contains 2 data, i tried retreiving it using string array the compiles without an error but while executing it gives NULL POINTER EXCEPTION. I think i did some mistake while retreiving it so please can anyone give proper code to retreive data ???
The error message is as below,

[SRC]# java UDPServer
Opening a connection to database
Starting to listen for data to persist to database.
entering main
configuration is done
connection got
query executed
Coefficent 1 is :0.032 // this 2 are retrieved data from DB
Coefficent 0 is :-23.82
Device ID is : 201001000006
Sensor type is : HUM
exception occured
java.lang.NullPointerException
at FetchCoeff.testing(FetchCoeff.java:51)
at UDPServer.main(UDPServer.java:70)
Exception in thread "main" java.lang.NullPointerException
at UDPServer.main(UDPServer.java:72)



The 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[] = null;


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 here i am getting NULL POINTER EXCEPTION
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 ;
WOW !!! It worked Thank you very much for your time and help
really feeling very happy

the problem is i missed : and .
9 years ago
thanks for replying

I did what you said but still its giving same error message
9 years ago
Hi i am new to java and i have little knowledge of frameworks
I used Hibernate in my project and its works fine in windows but in linux if i compile its giving error: Could not find or load main class

check the below commands where i got stuck

--- created java_home and path -------
[root@saraswati ~]# echo $JAVA_HOME
/usr/java/jdk1.7.0_04/bin/java
[root@saraswati ~]# echo $PATH
/usr/lib/ccache:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.7.0_04/bin
[root@saraswati ~]# cd /home/syslab/Downloads/src







[root@saraswati src]# ls
------- these are the .java files-------
Converter.java DataStorageHandler.java Hibernate.cfg.xml SensorReadings.hbm.xml UDPServer.java
ConvertReadings.java FetchCoeff.java SensorMapping.java SensorReadings.java
[root@saraswati src]# javac *.java


---- i am setting classpath inorder to compile .java files. lib folder contains all required libraries------

[root@saraswati src]# export CLASSPATH=/home/syslab/Downloads/lib/*
[root@saraswati src]# javac *.java
[root@saraswati src]# java UDPServer
Error: Could not find or load main class UDPServer
------- ths is the error i got -------




[root@saraswati src]# java *
Error: Could not find or load main class Converter.class
--------------------------------------



-------if i unset the classpath the code will run but it fails because of missing library files -------
[root@saraswati src]# unset CLASSPATH
[root@saraswati src]# java UDPServer
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/HibernateException
at UDPServer.main(UDPServer.java:22)
Caused by: java.lang.ClassNotFoundException: org.hibernate.HibernateException
at java.net.URLClassLoader$1.run(URLClassLoader.java: 366)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:4 23)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 56)
... 1 more
------------- this is the error ------------------------------


can anyone solve this problem i am totally stuck in this

thanks in advance
9 years ago