-->
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.  [ 2 posts ] 
Author Message
 Post subject: BASIC Hibernate Tomcat JNDI Binding
PostPosted: Sat Nov 20, 2004 10:34 pm 
Newbie

Joined: Sat Aug 14, 2004 8:12 pm
Posts: 1
Hibernate version:
2.1
Mapping documents:
<hibernate-configuration>

<session-factory name="java:/hibernate/HibernateFactory">

<property name="connection.datasource">java:comp/env/jdbc/cssa</property>
<property name="connection.useUnicode">true</property>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<!-- c3p0 -->
<property name="connection.provider_class">net.sf.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property>
<property name="c3p0.max_size">112</property>
<property name="c3p0.max_statements">300</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.timeout">600</property>
.......
Code between sessionFactory.openSession() and session.close():
public class Base {
protected static SessionFactory sessionFactory;
protected Session session;
protected Transaction transaction;
public Base() throws HibernateException {
this.initHibernate();
}
protected void initHibernate() throws HibernateException {
if (sessionFactory==null){
sessionFactory = new Configuration().configure().buildSessionFactory();
}
}
protected void beginTransaction() throws HibernateException {
if (session==null || !session.isOpen()){
session = sessionFactory.openSession();
}
transaction = session.beginTransaction();
}
protected void endTransaction(boolean commit) throws HibernateException {
if (commit) {
transaction.commit();
} else {
transaction.rollback();
}
if (session!=null && session.isOpen())session.close();
}
}


Other classes merely extends Base class, for example:
public class StashAction extends Base{


public StashAction() throws HibernateException {
super();
}

public void createGem(Stash stash)throws HibernateException, InterruptedException {
Player player=new PlayerAction().visitPlayer(stash.getOwner());
player.setNewStashItems(player.getNewStashItems()+1);
this.beginTransaction();
try{
session.save(stash);
new PlayerAction().updatePlayer(player);
}catch(JDBCException e){
}finally{
this.endTransaction(true);
}
}

public void deleteGem(Stash stash) throws HibernateException, InterruptedException {
this.beginTransaction();
try{
session.delete(stash);
}catch (JDBCException e){
}finally{
this.endTransaction(true);
}
}

public List displayGems(String identity) throws HibernateException, InterruptedException{
List list=new List();
Player player=new PlayerAction().visitPlayer(identity);
player.setNewStashItems(0);
this.beginTransaction();
try{
Query query = session.createQuery("select c from Stash as c where owner = :owner order by timeStamp");
query.setString("owner",identity);
for (Iterator it=query.iterate();it.hasNext();){
list = new List(it.next(),list);
}
}catch(JDBCException e){
}finally{
this.endTransaction(true);
}
new PlayerAction().updatePlayer(player);
return list;
}
}
Full stack trace of any exception that occurs:
02:00:50,296 WARN SessionFactoryObjectFactory:98 - Could not bind factory to JNDI
javax.naming.NamingException: Context is read only
at org.apache.naming.NamingContext.checkWritable(NamingContext.java:901)
at org.apache.naming.NamingContext.createSubcontext(NamingContext.java:515)
at org.apache.naming.NamingContext.createSubcontext(NamingContext.java:536)
at net.sf.hibernate.util.NamingHelper.bind(NamingHelper.java:69)
at net.sf.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)
at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:172)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:768)
at kairan.forum.logic.Base.initHibernate(Base.java:30)
at kairan.forum.logic.Base.<init>(Base.java:26)
at kairan.forum.logic.CityAction.<init>(CityAction.java:25)
at org.apache.jsp.forum.index_jsp._jspService(index_jsp.java:55)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
Name and version of the database you are using:
Mysql 4
The generated SQL (show_sql=true):
Irrelevant








I am a beginner in web programming, I read through the mannual many times and bought hibernate in action and read it through but I could not find the exact answer I need. I am only starting, so please bear with my stupid question.


My question is:
1 How To Bind Hibernate to TOMCAT's JNDI?
2 Is This binding necessary? Because if i just ignore it,(do no specify it) it will still work and only give me a warning saying JNDI is not specified, and even if I speicify the read-only version, It still works somehow so I dont' understand
3 How important is JNDI to any hibernate application,
4 Does it have any potential impact on the application if ignored.


For question 1, I have seen the post illustrating the two method one is by implementing threadlocal design pattern and one is Bertrand Lancelot's LET TOMCAT DO THE WORK solution.

I want to try the threadlocal method, I have also looked through the link to IBM's website but i do not understand how does it work and how does the fragment fit to my structure.

Please help, Hibernate is wonderful and I know you guys have a lot of things to do, but please be patient and give me some hint, I would appreciate any help even if it is a recommendation of a book.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 20, 2004 11:11 pm 
Regular
Regular

Joined: Sat Aug 28, 2004 4:15 pm
Posts: 61
As I understand it, Tomcat has a read only JNDI context which means you cannot bind to it. Thus, the threadlocal pattern is very effective. However, that isnt for the SessionFactory itself. There should be only 1 instance of the SessionFactory for the entire application. So, just make it statically accessible. Look to HibernateUtil.java from CaveatEmptor.


Also, read everything you can get a hold of about Hibernate. There is a ton of excellent online help and the Hibernate in Action book is solid.


Good luck,

Joe

_________________
Joe W


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