• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

single SessionFactory object

 
Harathi Rao
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As everybody knows, its good to maintain single SessionFactory object per application. I did this through static block. Plese suggest me whether its a good approach or not.


public class DBStore
{
static SessionFactory sessionFactory;
Session session;
Transaction tx;
static
{
sessionFactory = new Configuration().configure().buildSessionFactory();
}
public DBStore()
{
session = sessionFactory.openSession();
tx = session.beginTransaction();
}
public void save(Object obj)
{
CommonBean cbean = (CommonBean)obj;
session.save(cbean);
}

public void close()
{
tx.commit();
session.close();
}
}

thanks
Harathi
 
vasu kola
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

U r right, u can also create a static class where u have methods to accept configaration file as parameter, this will build a sessionfactory depending on the config(cfg) file u passed any how best approach buddy...
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's your context? Web application? Are you using other frameworks, like Spring?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic