-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: problems with refresh or multiple connections
PostPosted: Thu Dec 15, 2011 7:48 pm 
Newbie

Joined: Thu Dec 15, 2011 7:25 pm
Posts: 4
Hi all,
first, sorry for my english, is not my native language, I will try to explain my problem.

my problem is that the data structure of Hibernate does not update correctly
I have a HibernateUtil class with this code.
Code:
public class HibernateUtil {
  private static final SessionFactory sessionFactory = buildSessionFactory();
  private static SessionFactory buildSessionFactory(){
      try {
         return new Configuration().configure(new File("hibernate.cfg.xml")).buildSessionFactory();
      }catch(Exception ex){
         throw new ExceptionInInitializerError(ex);
      }
   }
   public static SessionFactory getSessionFactory() {
      return sessionFactory;
   }
}

with this class I use the sessionFactory global variable in all code

then I have a loop to do inserts on the DB, but the insertion is conditional.
I have an array with this structure [[year,value],.....] and this data
[
['2011','value1'],
['2010','value1'],
['2011','value1'],
.
.
.
.
.
]

then I try to insert these values on my DB, but before to insert I check if there is the year on my DB
Code:
//first verify if there is data on my DB
public Data ObtenerDatosDeData(String sYear){
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      Data objData = (Data)session.createQuery("select dt from datas as dt where dt.year=:dtrequestyear")
                        .setParameter("dtrequestyear", sYear)
                        .uniqueResult();
      session.getTransaction().commit();
      return objData;
}

this function always return an empty list for year 2011
why this happend??
how can I solve the problem??
thanks in advance for your help.

Quote:
added


I noticed something else.
if I insert data from another application (phpMyAdmin, other java app, ....)
the same problem occurs, it seems to me that get data from memory and not from the DB


Last edited by naitsir on Fri Dec 16, 2011 9:12 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: problems with refresh or multiple connections
PostPosted: Fri Dec 16, 2011 9:05 am 
Newbie

Joined: Thu Dec 15, 2011 7:25 pm
Posts: 4
Hi all,
I did a changes to the HibernateUtil class
Code:
public class HibernateUtil {
   private static SessionFactory buildSessionFactory(){
      try {
         return new Configuration().configure(new File("hibernate.cfg.xml")).buildSessionFactory();
      }catch(Exception ex){
         throw new ExceptionInInitializerError(ex);
      }
   }
   public static SessionFactory getSessionFactory() {
      SessionFactory sessionFactory = buildSessionFactory();
      return sessionFactory;
   }
}

with this change, every time that I call "getSessionFactory" function, creates a new instance of the sessionFactory variable.
the problem is, when I using this code to insert/select/update... multiple rows (in a loop)
Hibernate throw an exception when I beginTransaction()
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();

Code:
Cannot open connection
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
sun.reflect.GeneratedMethodAccessor102.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345)
$Proxy0.beginTransaction(Unknown Source)
db.clsNode.NuevoEditarNodo(clsNode.java:25)
biosis.XmlUPI_to_BD.GuardarDatosNodo(XmlUPI_to_BD.java:102)
biosis.XmlUPI_to_BD.parsearNodes(XmlUPI_to_BD.java:279)
biosis.XmlUPI_to_BD.parsearNodes(XmlUPI_to_BD.java:282)
biosis.XmlUPI_to_BD.parsearNodes(XmlUPI_to_BD.java:282)
biosis.XmlUPI_to_BD.parsearNodes(XmlUPI_to_BD.java:282)
biosis.XmlUPI_to_BD.parsearNodes(XmlUPI_to_BD.java:282)
biosis.XmlUPI_to_BD.guardarConfiguracion(XmlUPI_to_BD.java:261)
biosis.Biosis.main(Biosis.java:220)


looking for a bit, I'm assuming Hibernate is connected each time to the DB, comes a time when the DB does not allow more conecctions.

I have two solutions that I think are possible
for my first post:
- each time that I call the "getSessionFactory" function, get data directly from the DB (gives me the impression that uses data from memory)
for this post:
- after calling "session.beginTransaction();" function and do what has to be done, close the connection to the DB. I used the "session.close ();" function, but it does not work as I hope :(

please help me


Top
 Profile  
 
 Post subject: Re: problems with refresh or multiple connections
PostPosted: Fri Dec 16, 2011 9:57 am 
Newbie

Joined: Thu Dec 15, 2011 7:25 pm
Posts: 4
hi all, I again :P

I'm using my second implementation, but after to do all work I add this code
Code:
session.getSessionFactory().close();

apparently this works and gives me no erros :)

Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
....
....
....
session.getTransaction().commit();
session.getSessionFactory().close();

but I'm not sure if this is the optimal way of doing.

I will continue waiting for your answers.


Top
 Profile  
 
 Post subject: Re: problems with refresh or multiple connections
PostPosted: Mon Dec 19, 2011 9:09 am 
Newbie

Joined: Thu Dec 15, 2011 7:25 pm
Posts: 4
is not there anyone who can answer my questions?
:-S


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.