-->
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.  [ 8 posts ] 
Author Message
 Post subject: [newbie] Hinernates writes to, but not reads from db
PostPosted: Wed Feb 23, 2005 8:46 am 
Newbie

Joined: Wed Dec 01, 2004 2:08 pm
Posts: 8
I tried to find the answer in the docs here and at other places. Was not successful.

When I write a dataset this way:
BaseMyObjectDAO.getInstance().save( myObject );

it will appear in the db[mySQL] (which I read out with other means);

When I try to read the dataset, Hibernate will not find it:MyObject mo = BaseMyObjectDAO.getInstance().load( myObjectid );

Not transactions are involved.
I also tried to flush the buffer after the save-statement:
BaseMyObjectDAO.getInstance().createSession().flush();
No change.

Can anybody tell me what to do?

Thanks for your hints.
Klaus


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 23, 2005 12:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Check the generated SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 24, 2005 5:54 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
A few things:

Quote:
Not transactions are involved.

Why are you avoiding the use of transactions? Always work within the confines of a transaction, even when reading.

Quote:
When I try to read the dataset, Hibernate will not find it:MyObject mo = BaseMyObjectDAO.getInstance().load( myObjectid );

Is mo null or are you just not seeing generated SQL? You object may be proxied and therefore the actual load may not be happening yet. Try session.get()


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 26, 2005 2:03 pm 
Newbie

Joined: Wed Dec 01, 2004 2:08 pm
Posts: 8
drj wrote:
A few things:

Quote:
Not transactions are involved.

Why are you avoiding the use of transactions? Always work within the confines of a transaction, even when reading.

Quote:
When I try to read the dataset, Hibernate will not find it:MyObject mo = BaseMyObjectDAO.getInstance().load( myObjectid );

Is mo null or are you just not seeing generated SQL? You object may be proxied and therefore the actual load may not be happening yet. Try session.get()


1) I do not avoid transactioning, I just did'nt do anything special to Hibernate. I am using the Eclipse Hibernate as described there. But, I cannot see any transactional calls in the SQL trace.

2) Actually, I can see the "insert" statement in the sql statement. At this very point I see the row in the table of mySQL with my SQL tool.
After that, when I call the same dataset with Hibernate (I call it by Id), I can see the "select" statement, but in my code, it throws an "ObjectNotFoundException" with the message "No row with the given identifier exists". BUT THE DATABASE HAS IT!

3) When I perform another write-statement, the previous mentioned set is availale to me.

I do flush the session after the insert-operation, what else can I do???

Thanks for your kind help

Klaus


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 27, 2005 5:49 am 
Newbie

Joined: Wed Dec 01, 2004 2:08 pm
Posts: 8
In the meantime, I have dragged out the SQL query out of the Hibernate-log and did the following:

1) I executed the query with hibernate and got the Exception "No Row with the given identifier".

2) I took the original Hibernate SQL query from the log and replaced the questionmark from "where customer0_=?" with the real id and executed the very same statement in my SQL tool. And got a propper result.

3) To be shure, I executed 1) again with the same exception.

This behavior seems to be constant until I execute a writing action. Then, I can read the old row(s) except for the newly written...

I will be truly grateful for any suggestion.

Thanks
Klaus

--------------------------------------------------------------------------
Not to neglect a thing, here my hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory>
<!-- local connection properties -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/dll?useUnicode=true
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.username">xxxxxxx</property>
<property name="hibernate.connection.password">xxxxx</property>
<property name="hibernate.connection.pool_size">50</property>
<!-- dialect for MySQL -->
<property name="dialect">
net.sf.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_outer_join">true</property>

<mapping resource="Customers.hbm" />
<!=... and others...-->

</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 11:48 am 
Newbie

Joined: Wed Dec 01, 2004 2:08 pm
Posts: 8
Getting more into it, I can describe additionally:

1) I write and read via a servlet, where I do a "_BaseRootDAO.initialize();" centrally in the init-method. But I also tried to do this initialization before any db-call.

2) I write my dataset to the db and - for testing reasons - read it immediately after that. This works stable.

3) Then I leave the servlet and re-enter it with a new read-statement. This will fail with the HibernateException "No row with the given identifier exists". At the same time, I can access this very row with a load from my Eclipse-based testenvironment.

The servlet is basically very simple:

Code:
public void init(ServletConfig config) throws ServletException {
      super.init(config);
   
      try {
         _BaseRootDAO.initialize();
      } catch (HibernateException e1) {
         System.out.println( "Exception initializing Hibernate ProcessOrderSERVLET.init. Msg: " + e1.getMessage() );
      }
}


protected void service(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {

    String reqPar = request.getParameter("request");
      
   if (reqPar.equals("writeit")) {
       writeIt(request, response);
    } else
    if (reqPar.equals("readit")) {
       readIt(request, response);
    }
}

private void writeIt(HttpServletRequest request,
         HttpServletResponse response){
   MyObject mo = new MyObject();
   ... populate object ...


  Integer IdStr = BaseMyObjectDAO.getInstance().save( mo );

  //for testing... (works)
  MyObject o = BaseMyObjectDAO.getInstance().load( ord.getId() );
  try{
     System.out.println( "RELOAD ID: " + o.getId() );
  } catch (Exception e){
      System.out.println( "Could not be loaded: Id: " + ord.getId().intValue() );
  }
}

private void readIt(HttpServletRequest request,
         HttpServletResponse response){
  try{
   MyObject obj = BaseMyObjectDAO.getInstance().load( new Integer( request.getParameter("id" ) ) );
   ... do something with it ...
  } catch ( HibernateException e ){
      //if I call the just saved row, it will alway jump to this point when there was no other writing action inbetween
  }
}


The behavior is sooo confusing...

Thanks for any valueable thought.

Klaus


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 05, 2005 9:00 am 
Newbie

Joined: Wed Dec 01, 2004 2:08 pm
Posts: 8
To round up the issue, I would like to share the solution for the described problem (I spent tons of time with this :-( ).

Apparently, the mySQL -with InnoDB- there is a problem with the isolation level of the transactions. The link: http://netmirror.org/mirror/mysql.com/d ... ation.html (observe the comment on the lower end of the page)

I switched my database simply from InnoDB to MyIsam and solved this nasty problem.

So: WHENEVER YOU WANT TO CALL mySQL FROM A SERVLET, DONT USE INNODB!

Klaus


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 06, 2005 9:44 pm 
Beginner
Beginner

Joined: Thu Jul 08, 2004 2:21 pm
Posts: 20
Location: Toronto
kkaal wrote:
I switched my database simply from InnoDB to MyIsam and solved this nasty problem.

So: WHENEVER YOU WANT TO CALL mySQL FROM A SERVLET, DONT USE INNODB!
Klaus

Nothing wrong with repeatable-read isolation level.

But you are right, it is a nasty thing.
For example you can retrieve in one session object that been deleted in another session.

So if you are getting similar behaviour check you database isolation level. Regardless of your database server.
After, check connection pool for your application. You might have uncommited transactions if JDBC auto commit set to false and you do not define transaction scope by beginnig and commiting transactions in your code.
Also I highly advice you to use transaction whenever you do _any_ changes. It doesn't heart but definitely helps a lot.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.