-->
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.  [ 7 posts ] 
Author Message
 Post subject: Session and c3p0 problem!!
PostPosted: Sat Oct 11, 2008 10:16 am 
Newbie

Joined: Sat Oct 11, 2008 7:28 am
Posts: 10
Location: New Delhi (INDIA)
Hi Friends,

I am a newbie to hibernate. Please consider the following info first:

Hibernate version:3.1

Mapping documents:
Code:
<hibernate-mapping>
<class name="com.raisonne.example.Honey" table="honey">
   <id name="id" column="HONEY_ID" type="java.lang.Long">
   <generator class="increment"/>
   </id>
   <property name="name" column="NAME" type="java.lang.String"/>
   <property name="taste" column="TASTE" type="java.lang.String"/>
   
    <idbag name="wives" table="wives" order-by="WIFE_INDEX asc" >      
       <collection-id type="long" column="WIFE_ID">
       <generator class="increment">
       </generator>
       </collection-id>
      <key column="HONEY_ID" not-null="true"></key>       
       <composite-element class="com.raisonne.example.Wives">
          <parent name="Honey"/>
         <property name="name" column="NAME"></property>
         <property name="color" column="COLOR"></property>
         <property name="wife_index" column="WIFE_INDEX"></property>
      </composite-element>
      
   </idbag>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Code:
   public static void saveCollection(Collection<Honey> honeys){
      Transaction tx =null;
      
      try {
         
         for (Iterator<Honey> iter =honeys.iterator();iter.hasNext();){
            Honey singleHoney =(Honey)iter.next();
            Session session =InitSessionFactory.getInstance().getCurrentSession();
            tx =session.beginTransaction();
            session.save(singleHoney);               
            tx.commit();
            session.flush();
            session.close();               
         }         
         }catch (HibernateException e){
            e.printStackTrace();
            if (tx !=null &&tx.isActive())
            tx.rollback();
            }finally{
               InitSessionFactory.close();   
            }
   }

Name and version of the database you are using:[MySql 5.0/b]

[b]The generated SQL (show_sql=true) and Debug level Hibernate log excerpt:



Code:
19:21:41,156 INFO org.hibernate.impl.SessionFactoryObjectFactory {1}:82 -Not binding factory to JNDI, no JNDI name configured
19:21:41,671DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:1644 -trace com.mchange.v2.resourcepool.BasicResourcePool@15e2ccd [managed: 5, unused: 4, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1d381d2)
19:21:41,687DEBUG org.hibernate.SQL {1}:346 -select max(HONEY_ID) from honey
Hibernate: select max(HONEY_ID) from honey
19:21:41,765DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:457 -cxnStmtMgr.statementSet( com.mysql.jdbc.Connection@3c9c31 ).size(): 1
19:21:41,765DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:196 -checkoutStatement: com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 1; checked out: 1; num connections: 1; num keys: 1
19:21:41,875DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:271 -checkinStatement(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 1; checked out: 0; num connections: 1; num keys: 1
19:21:42,062DEBUG org.hibernate.SQL {1}:346 -/* insert com.raisonne.example.Honey */ insert into honey (NAME, TASTE, HONEY_ID) values (?, ?, ?)
Hibernate: /* insert com.raisonne.example.Honey */ insert into honey (NAME, TASTE, HONEY_ID) values (?, ?, ?)
19:21:42,078DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:457 -cxnStmtMgr.statementSet( com.mysql.jdbc.Connection@3c9c31 ).size(): 2
19:21:42,078DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:196 -checkoutStatement: com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 2; checked out: 1; num connections: 1; num keys: 2
19:21:42,078DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:271 -checkinStatement(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 2; checked out: 0; num connections: 1; num keys: 2
19:21:42,078DEBUG org.hibernate.SQL {1}:346 -select max(WIFE_ID) from wives
Hibernate: select max(WIFE_ID) from wives
19:21:42,078DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:457 -cxnStmtMgr.statementSet( com.mysql.jdbc.Connection@3c9c31 ).size(): 3
19:21:42,078DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:196 -checkoutStatement: com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 3; checked out: 1; num connections: 1; num keys: 3
19:21:42,078DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:271 -checkinStatement(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 3; checked out: 0; num connections: 1; num keys: 3
19:21:42,078DEBUG org.hibernate.SQL {1}:346 -/* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
Hibernate: /* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
19:21:42,093DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:457 -cxnStmtMgr.statementSet( com.mysql.jdbc.Connection@3c9c31 ).size(): 4
19:21:42,093DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:196 -checkoutStatement: com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 4; checked out: 1; num connections: 1; num keys: 4
19:21:42,093DEBUG org.hibernate.SQL {1}:346 -/* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
Hibernate: /* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
19:21:42,093DEBUG org.hibernate.SQL {1}:346 -/* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
Hibernate: /* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
19:21:42,093DEBUG org.hibernate.SQL {1}:346 -/* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
Hibernate: /* insert collection row com.raisonne.example.Honey.wives */ insert into wives (HONEY_ID, WIFE_ID, NAME, COLOR, WIFE_INDEX) values (?, ?, ?, ?, ?)
19:21:42,109DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:271 -checkinStatement(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 4; checked out: 0; num connections: 1; num keys: 4
19:21:42,125DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:297 -checkinAll(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 4; checked out: 0; num connections: 1; num keys: 4
19:21:42,125DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:1644 -trace com.mchange.v2.resourcepool.BasicResourcePool@15e2ccd [managed: 5, unused: 4, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@1d381d2)
19:21:42,125DEBUG com.mchange.v2.c3p0.stmt.GooGooStatementCache {1}:297 -checkinAll(): com.mchange.v2.c3p0.stmt.GlobalMaxOnlyStatementCache stats -- total size: 4; checked out: 0; num connections: 1; num keys: 4
19:21:42,140 INFO org.hibernate.impl.SessionFactoryImpl {1}:729 -closing
19:21:42,156DEBUG com.mchange.v2.c3p0.management.ActiveManagementCoordinator {1}:133 -MBean: com.mchange.v2.c3p0:type=PooledDataSource[1hge13l7w1tj3nio1v6twje|1ff92f5] unregistered.
19:21:42,156DEBUG com.mchange.v2.async.ThreadPoolAsynchronousRunner {1}:578 -Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main] interrupted. Shutting down.
19:21:42,156DEBUG com.mchange.v2.async.ThreadPoolAsynchronousRunner {1}:578 -Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main] interrupted. Shutting down.
19:21:42,156DEBUG com.mchange.v2.c3p0.management.ActiveManagementCoordinator {1}:85 -C3P0Registry mbean unregistered.
[u]org.hibernate.SessionException: Session is closed![/u]
   at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:49)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:978)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
   at $Proxy0.flush(Unknown Source)
   at com.raisonne.example.TestExample.saveCollection(TestExample.java:192)
   at com.raisonne.example.TestExample.main(TestExample.java:52)
19:21:42,171DEBUG com.mchange.v2.async.ThreadPoolAsynchronousRunner {1}:578 -Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main] interrupted. Shutting down.
19:21:42,171DEBUG com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource {1}:417 -com.mchange.v2.c3p0.PoolBackedDataSource@1ff92f5 has been closed.
[u]java.lang.Exception: DEBUG STACK TRACE for [/u]PoolBackedDataSource.close().
   at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:417)
   at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:429)
   at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:359)
   at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:335)
   at org.hibernate.connection.C3P0ConnectionProvider.close(C3P0ConnectionProvider.java:118)
   at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:759)
   at com.raisonne.hibernate.InitSessionFactory.close(InitSessionFactory.java:108)
   at com.raisonne.example.TestExample.saveCollection(TestExample.java:200)
   at com.raisonne.example.TestExample.main(TestExample.java:52)
19:21:42,171DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:962 -Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb
19:21:42,218DEBUG com.raisonne.example.TestExample {1}:65 -com.raisonne.example.Honey@15db13f
19:21:42,218DEBUG com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool {1}:468 -Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb
19:21:42,218DEBUG com.raisonne.example.TestExample {1}:66 -com.raisonne.example.Honey@1ab2b55
19:21:42,218DEBUG com.mchange.v2.c3p0.impl.NewPooledConnection {1}:566 -com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb closed by a client.
java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE
   at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:566)
   at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:234)
   at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.destroyResource(C3P0PooledConnectionPool.java:470)
   at com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:964)
   at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:989)
   at com.mchange.v2.resourcepool.BasicResourcePool.access$100(BasicResourcePool.java:32)
   at com.mchange.v2.resourcepool.BasicResourcePool$5.run(BasicResourcePool.java:1174)
19:21:42,218DEBUG com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool {1}:476 -Successfully destroyed PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb
19:21:42,234DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:967 -Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb
19:21:42,234DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:962 -Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@189c036
19:21:42,234DEBUG com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool {1}:468 -Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@189c036
19:21:42,234DEBUG com.mchange.v2.c3p0.impl.NewPooledConnection {1}:566 -com.mchange.v2.c3p0.impl.NewPooledConnection@189c036 closed by a client.


I am try to store collection of data related to two entity classes i.e. Honey and Wives.
The common protocol is that: Honey can have no. of wives but a wife can only have one honey.
But in my application the wives table is not storing honey_id properly.

Please have a look into the log excerpt and any suggestions are most welcome.

_________________
Thanks,
Arun


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 11, 2008 4:09 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
You cannot create a SessionFactory for every database activity.

The SessionFactory should be a static singleton, or something of the like, that is unique per database and has an application-wide scope.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2008 5:31 am 
Newbie

Joined: Sat Oct 11, 2008 7:28 am
Posts: 10
Location: New Delhi (INDIA)
Thanks! Gonzalo,

Following your advice I made some improvements in my code and its better now but on encountering below line of code the below mentioned excption is occuring in log, can you please guide me regarding this issue:
CODE:
Code:
InitSessionFactory.close();


LOG:
Code:
14:52:56,104 INFO org.hibernate.impl.SessionFactoryImpl {1}:729 -closing
14:52:56,104DEBUG com.mchange.v2.c3p0.management.ActiveManagementCoordinator {1}:133 -MBean: com.mchange.v2.c3p0:type=PooledDataSource[1hge13l7w1uoxx4x19dvxo2|9505f] unregistered.
14:52:56,104DEBUG com.mchange.v2.async.ThreadPoolAsynchronousRunner {1}:578 -Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main] interrupted. Shutting down.
14:52:56,104DEBUG com.mchange.v2.async.ThreadPoolAsynchronousRunner {1}:578 -Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main] interrupted. Shutting down.
14:52:56,104DEBUG com.mchange.v2.c3p0.management.ActiveManagementCoordinator {1}:85 -C3P0Registry mbean unregistered.
14:52:56,120DEBUG com.mchange.v2.async.ThreadPoolAsynchronousRunner {1}:578 -Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main] interrupted. Shutting down.
14:52:56,120DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:962 -Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@13936e1
14:52:56,120DEBUG com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool {1}:468 -Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@13936e1
14:52:56,120DEBUG com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource {1}:417 -com.mchange.v2.c3p0.PoolBackedDataSource@9505f has been closed.
[u]java.lang.Exception: DEBUG STACK TRACE for [/u]PoolBackedDataSource.close().
   at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:417)
   at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.close(AbstractPoolBackedDataSource.java:429)
   at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:359)
   at com.mchange.v2.c3p0.DataSources.destroy(DataSources.java:335)
   at org.hibernate.connection.C3P0ConnectionProvider.close(C3P0ConnectionProvider.java:118)
   at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:759)
   at com.raisonne.hibernate.InitSessionFactory.close(InitSessionFactory.java:108)
   at com.raisonne.example.TestExample.saveCollection(TestExample.java:209)
   at com.raisonne.example.TestExample.main(TestExample.java:52)
14:52:56,120DEBUG com.mchange.v2.c3p0.impl.NewPooledConnection {1}:566 -com.mchange.v2.c3p0.impl.NewPooledConnection@13936e1 closed by a client.
[u]java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE[/u]
   at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:566)
   at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:234)
   at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.destroyResource(C3P0PooledConnectionPool.java:470)
   at com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:964)
   at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:989)
   at com.mchange.v2.resourcepool.BasicResourcePool.access$100(BasicResourcePool.java:32)
   at com.mchange.v2.resourcepool.BasicResourcePool$5.run(BasicResourcePool.java:1174)
14:52:56,120DEBUG com.raisonne.example.TestExample {1}:65 -com.raisonne.example.Honey@15db13f
14:52:56,120DEBUG com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool {1}:476 -Successfully destroyed PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@13936e1
14:52:56,120DEBUG com.raisonne.example.TestExample {1}:66 -com.raisonne.example.Honey@1c783c5
14:52:56,120DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:967 -Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@13936e1
14:52:56,120DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:962 -Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb
14:52:56,120DEBUG com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool {1}:468 -Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb
14:52:56,135DEBUG com.mchange.v2.c3p0.impl.NewPooledConnection {1}:566 -com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb closed by a client.
[u]java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE[/u]
   at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:566)
   at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:234)
   at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.destroyResource(C3P0PooledConnectionPool.java:470)
   at com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask.run(BasicResourcePool.java:964)
   at com.mchange.v2.resourcepool.BasicResourcePool.destroyResource(BasicResourcePool.java:989)
   at com.mchange.v2.resourcepool.BasicResourcePool.access$100(BasicResourcePool.java:32)
   at com.mchange.v2.resourcepool.BasicResourcePool$5.run(BasicResourcePool.java:1174)
14:52:56,135DEBUG com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool {1}:476 -Successfully destroyed PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb
14:52:56,135DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:967 -Successfully destroyed resource: com.mchange.v2.c3p0.impl.NewPooledConnection@933bcb
14:52:56,135DEBUG com.mchange.v2.resourcepool.BasicResourcePool {1}:962 -Preparing to destroy resource: com.mchange.v2.c3p0.impl.NewPooledConnection@141fab6
14:52:56,135DEBUG com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool {1}:468 -Preparing to destroy PooledConnection: com.mchange.v2.c3p0.impl.NewPooledConnection@141fab6
14:52:56,135DEBUG com.mchange.v2.c3p0.impl.NewPooledConnection {1}:566 -com.mchange.v2.c3p0.impl.NewPooledConnection@141fab6 closed by a client.
[u]java.lang.Exception: DEBUG -- CLOSE BY CLIENT STACK TRACE[/u]
   at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:566)
   at com.mchange.v2.c3p0.impl.NewPooledConnection.close(NewPooledConnection.java:234)

_________________
Thanks,
Arun


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 12, 2008 6:28 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
No, no. You usually don't close the sessionfactory explicitly.
It should remain alive throughout the life of your application.

If it is a web application, you should use some contextual sessions' strategy that allows you to get always a good session by calling yourSessionFactory.getCurrentSession().
(I, for example, use Spring with OSIV).

But yourSessionFactory remains one and the same, during the lifespan of your application.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 13, 2008 1:56 pm 
Newbie

Joined: Sat Oct 11, 2008 7:28 am
Posts: 10
Location: New Delhi (INDIA)
Thanks again!! [:)]

As I have stored and retrieved collection of objects using hibernate in a simple application and its working fine now.
Now I am trying to integrate hibernate in Struts2. I tried it on my own but
Code:
sessionFactory =cfg.buildSessionFactory();
is throwing null pointer exception. I am not getting the reason.
I haven't done anything special to configure hibernate with struts2, just placed the requied jars and hibernate configuration files and trying to run it, but not getting success.

Can u guide me again [;)]

_________________
Thanks,
Arun


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 13, 2008 2:45 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Certainly.

In Struts you have to use some solution akin to the HibernateUtil class given in the documentation, because you cannot use declarative transactions nor contextual sessions in struts.

You can find a simple HibernateUtil class in:
(1.4. Playing with cats)

So, basically, every time you need a session, you obtain it from this HibernateUtil. You can create an additional DAO helper class that features basic
insert(Object),
update(Object),
select(Sql string)
delete(Id)
methods, and this helper class would wrap every one of those calls in a transaction, which is needed by Hibernate.

For example, an insert() method ...

Code:
   
public class DAO {

...

protected Object insert(Object candidate) throws ValidationException,
            PersistException {
        Object id = null;
        try {
            logger.debug("(create) instance of "
                    + candidate.getClass().getName());
            Session session = HibernateUtil.getSession();
            HibernateUtil.beginTransaction();

            id = session.save(candidate);

            commit();
            logger.debug("(create) created id=" + id);

        } catch (HibernateException e) {
            // Log the exception
            logger.error("(create) candidate=" + candidate, e);
            rollback();
            throw new PersistException("Create failed!", candidate, e);
        }catch (Exception e){
            logger.error("some other exception inserting a collection=", e);
            throw new PersistException("Create failed!", candidate, e);
        }

        return candidate;
    }


Then you call this insert() method every time you need to insert something from your business objects.
Code:
public class BusinessObject extends DAO {
...

public void createPerson(PersonBean bean){
   //some business stuff here
   super.create(bean);
}


All of this is crude, but it is the most you can get in Struts.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject: hibernate with struts2
PostPosted: Fri Oct 17, 2008 1:12 pm 
Newbie

Joined: Sat Oct 11, 2008 7:28 am
Posts: 10
Location: New Delhi (INDIA)
In Struts2 I am getting the following log message when encountering the line
Code:
session =InitSessionFactory.getSessionFactory().openSession();

log:
Code:
Initial SessionFactory creation failed.java.lang.NoSuchMethodError: org.hibernate.util.ReflectHelper.getFastClass(Ljava/lang/Class;)Lnet/sf/cglib/reflect/FastClass;

I also tried to locate the method "org.hibernate.util.ReflectHelper.getFastClass()" in jar file but it was not there!!...
In simple application its working fine but in struts its not.

please review the following class
Code:
package com.dograpress.raisonne.persistence;


import org.hibernate.*;
import org.hibernate.cfg.*;

public class InitSessionFactory {   
   private static final SessionFactory sessionFactory;
    static {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
   
}


please suggest!!...i m really strucked with this issue....

_________________
Thanks,
Arun


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