-->
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: Singleton Hibernate for Struts
PostPosted: Sat Jan 29, 2005 11:59 pm 
Newbie

Joined: Sat Jan 08, 2005 6:26 pm
Posts: 2
Location: Brazil, Parana, Maringa
Hi,

I found a lot of connection classes made with singleton,
for hibernate, including the one in the hibernate site.
But I need one that works with Struts' Hibernate Plugin...
Does someone have one??!? PLEASE

_________________
João Eduardo Galli
realm@brturbo.com


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 30, 2005 5:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
what do you mean by
Quote:
connection classes
?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject: connection classes
PostPosted: Sun Jan 30, 2005 12:05 pm 
Newbie

Joined: Sat Jan 08, 2005 6:26 pm
Posts: 2
Location: Brazil, Parana, Maringa
sorry Im a little new...

I mean a class that create a connection with the db,
and returns you a Session...

I got one class of these, with singleton, but not using the struts' hibernate plugin, here it is:
Code:
/*
* HibernateSessionFactory.java
*
* Created on 30 de Janeiro de 2005, 01:13
*/

package controller;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
/**
*
* @author Realm
*/
/**
* Encapsula a criação de sessões para o Hibernate.
* Utiliza uma política ThreadLocal para fornecer as sessões.
*
* @author Paulo Silveira
*/
public class HibernateSessionFactory {
   private static HibernateSessionFactory instance;
   private SessionFactory sessionFactory;
   private LocalSessionManager sessionManager = new LocalSessionManager();
   
   /**
    * Pega uma sessão do Hibernate.
    * Talvez essa sessão seja aberta agora, caso a Thread.currentThread() nunca tenha
    * pedido uma sessão ainda.
    *
    * @return uma sessão do hibernate.
    */
   public Session getSession() {
      Session session = (Session) this.sessionManager.get();
      return session;
   }
   
       
   /**
    * Construtor privado para o singleton
    *
    * @throws IOException
    * @throws HibernateException
    */
   private HibernateSessionFactory(Object sf) throws HibernateException {
      Configuration cfg = new Configuration();
      cfg.addClass(fotolog.model.Comentario.class);
      cfg.addClass(fotolog.model.Confirmacao.class);
      cfg.addClass(fotolog.model.Favorito.class);
      cfg.addClass(fotolog.model.Foto.class);
      cfg.addClass(fotolog.model.Preferencia.class);
                cfg.addClass(fotolog.model.Usuario.class);
               
                if(sf == null) {
                    this.sessionFactory = (SessionFactory)sf;
                } else {
                    this.sessionFactory = cfg.buildSessionFactory();
                }
   } 
       
   /**
    * Acesso ao singleton
    * @return o singleton desta classe
    */   
   public static HibernateSessionFactory getFactory() {
      if (instance == null) {
         try {
            instance = new HibernateSessionFactory(null);
         }
         catch (Exception e) {
            e.printStackTrace(System.err);
            throw new RuntimeException("problems while instantiating the singleton", e);
         }
      }
      return instance;
   }

   /**
    * Acesso ao singleton
    * @return o singleton desta classe
    */   
   public static HibernateSessionFactory getFactory(Object sf) {
      if (instance == null) {
         try {
            instance = new HibernateSessionFactory(sf);
         }
         catch (Exception e) {
            e.printStackTrace(System.err);
            throw new RuntimeException("problems while instantiating the singleton", e);
         }
      }
      return instance;
   }       
       
   /**
    * Classe utilizada para cuidar das sessões do hibernate, de uma maneira local a cada Thread.
    * Isto é, cada Thread usa apenas uma sessão.
    * @todo tirar essa classe do interior?
    * @author Paulo Silveira
    */   
   protected class LocalSessionManager extends ThreadLocal {
      
      /**
       * Devolve o valor inicial que deve ser dado a uma Thread quando
       * ela pedir uma Session. No nosso caso, esse valor nunca muda, é sempre
       * a mesma sessão para a mesma Thread.
       */
      protected Object initialValue() {
         Session session;
         try {
            session = HibernateSessionFactory.this.sessionFactory.openSession();
         }
         catch (HibernateException e) {
            throw new RuntimeException("Problems while opening a new thread local hibernate session", e);
         }
         return session;
      }
   }
}

_________________
João Eduardo Galli
realm@brturbo.com


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 30, 2005 12:51 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
ah ok, what if you decide to switch to another web framework?

it's better to use the servlet filter

http://www.hibernate.org/43.html

http://www.hibernate.org/42.html

or better, download caveatemptor
http://caveatemptor.hibernate.org/

and look how filter works

and forget struts plug in, not a good idea

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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.