-->
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.  [ 6 posts ] 
Author Message
 Post subject: about session.get and session.load method
PostPosted: Thu Nov 17, 2005 4:23 am 
Newbie

Joined: Tue Nov 15, 2005 8:00 am
Posts: 5
Location: china
my hibernate version is: 3.1.rc1

Hibernate version:
Hibernate-Version: 3.1.rc1

Code between sessionFactory.openSession() and session.close():
I use follow codes;

Session s = HibernateUtil.currentSession();

Transaction tx = s.beginTransaction();
//Person p = (Person) s.load(Person.class, new Integer(1)); // (1)
Person p = (Person) s.get(Person.class, new Integer(1));
tx.commit(); //why ?
System.out.println(p.getName());

HibernateUtil.closeSession();


i can use the method s.get() and there gos well
but if i use the method s.load and there will have something is wrong!
the full stack trace as fellows;
Full stack trace of any exception that occurs:
16:11:19,984 INFO SessionFactoryImpl:432 - Checking 0 named queries
16:11:20,093 ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session is disconnected
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session is disconnected
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:59)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:98)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:133)
at test.hibernate.Person$$EnhancerByCGLIB$$775d807d.getName(<generated>)
at test.hibernate.Test.main(Test.java:34)

who can tell me why can use s.get() but can not use s.load().

by the way.
my .cfg.xml file as follows
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">329</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.transaction.auto_close_session">false</property>

<mapping resource="test/hibernate/Person.hbm.xml"/>

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

_________________
Hibernate is good


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 4:37 am 
Beginner
Beginner

Joined: Sat Oct 22, 2005 11:16 pm
Posts: 40
session.load(id) will throw an exception if the object can't be found in the DB. Use load() only when you are certain that the object will be found. get() will return a null if the object cannot be found. Use get if you are not absolutely certain that the object is there.


Top
 Profile  
 
 Post subject: I know about this
PostPosted: Thu Nov 17, 2005 7:29 pm 
Newbie

Joined: Tue Nov 15, 2005 8:00 am
Posts: 5
Location: china
SleepyBear wrote:
session.load(id) will throw an exception if the object can't be found in the DB. Use load() only when you are certain that the object will be found. get() will return a null if the object cannot be found. Use get if you are not absolutely certain that the object is there.



i know about above:
see the follows code:

Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
System.out.println("session : isConnected:" + session.isConnected());
System.out.println("session : isOpen:" + session.isOpen());
tx.commit();
System.out.println("session : isConnected:" + session.isConnected());
System.out.println("session : isOpen:" + session.isOpen());

the result is:
session : isConnected:true
session : isOpen:true

session : isConnected:false
session : isOpen:true

i want to know why when i call tx.commit() ,and then the session is disconnected

_________________
Hibernate is good


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 17, 2005 8:16 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
maybe this can help ... in relation to the hibernate.connection.release_mode conf property .

Code:
Specify when Hibernate should release JDBC connections. By default, a JDBC connection is held until the session is explicitly closed or disconnected. For an application server JTA datasource, you should use after_statement to aggressively release connections after every JDBC call. For a non-JTA connection, it often makes sense to release the connection at the end of each transaction, by using after_transaction. auto will choose after_statement for the JTA and CMT transaction strategies and after_transaction for the JDBC transaction strategy.
eg. on_close (default) | after_transaction | after_statement | auto


Top
 Profile  
 
 Post subject: i try again in the version 3.0.5 and there is all right
PostPosted: Fri Nov 18, 2005 5:24 am 
Newbie

Joined: Tue Nov 15, 2005 8:00 am
Posts: 5
Location: china
in the version 3.0.5
there is no Exception

maybe this is a bug in the version of 3.1rc2

_________________
Hibernate is good


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 18, 2005 5:51 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
You need this in hibernate.cfg.xml to get the hibernate 3.0 behaviour:

Code:
<property name="hibernate.connection.release_mode">on_close</property>


This was modified in 3.1.


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