Latest Entries »

Tuesday, April 28, 2009

javax.naming.NameNotFoundException: Name java:comp is not bound in this Context


To resolve this exception, need to cross check context.xml, web.xml and jar files.

Creation of context.xml in your META-INF auto generates the xml file with the project name in TOMCAT/conf/ ( C:\Program Files\Apache Software Foundation\apache-tomcat-6.0.18\conf\Catalina\localhost ) cross check this file.

compare context.xml and web.xml with the reference files posted in my previous posting.









No need to create an entry in TOMCAT context.xml and server.xml files.

Finally you need to check your lib folders, both tomcat and project lib.

If you have the following jar's in your lib folder, then remove those

a) naming-common.jar
b) naming-factory.jar
c) naming-resources.jar

clean and rebuild your project and then deploy it, before starting tomcat delete xml file from C:\Program Files\Apache Software Foundation\apache-tomcat-6.0.18\conf\Catalina\localhost

Stack Trace:

javax.naming.NameNotFoundException: Name java:comp is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:765)
at org.apache.naming.NamingContext.lookup(NamingContext.java:147)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at com.sravan.As400Connection.getConnection(As400Connection.java:47)
at com.sravan.GetValuesFromDB.getViolationCodeQM(GetValuesFromDB.java:1054)
at com.sravan.InsertintoDB.insertFromIntofiles(InsertintoDB.java:746)
at com.parseWEB.Services.ProjectNameSoapBindingImpl.getRequest(ProjectNameSoapBindingImpl.java:545)
at com.parseWEB.Services.ProjectNameSoapBindingSkeleton.getRequest(ProjectNameSoapBindingSkeleton.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.axis.providers.java.RPCProvider.invokeMethod(RPCProvider.java:397)
at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:186)
at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)

Stack Trace of TOMCAT:

May 31, 2009 10:01:39 AM org.apache.tomcat.util.modeler.Registry registerComponent
SEVERE: Null component Catalina:type=DataSource,path=/ProjectName,host=localhost,class=javax.sql.DataSource,name="jdbc/DataSourceAS400"
May 31, 2009 10:01:39 AM org.apache.tomcat.util.modeler.Registry registerComponent
SEVERE: Null component Catalina:type=DataSource,path=/ProjectName,host=localhost,class=javax.sql.DataSource,name="jdbc/DataSource"

Sunday, April 5, 2009

Lazy initialization in Hibernate with icefaces JSF and Spring

To avoid hibernate lazy initialization there are different methods depending upon the project configuration. My project configuration is icefaces as a presentation layer, spring as a business layer and hibernates as a transaction layer.

Use the following class code as a listener and you need to register this class in faces-config.xml










package com.project.util;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;


public class OpenSessionInViewPhaseListener implements PhaseListener {

/**
*
*/
private static final long serialVersionUID = -3263031646854899605L;

@SuppressWarnings("unchecked")
private static ThreadLocal ts = new ThreadLocal();

private class ThreadData {
private boolean participate;
private Session session;
private SessionFactory sessionFactory;
private int beforeTimes;
private int afterTimes;
}

@SuppressWarnings("unchecked")
public void beforePhase(PhaseEvent pe) {
ThreadData td = (ThreadData) ts.get();
if (td == null) {
td = new ThreadData();
} else {
td.beforeTimes++;
return;
}
if (pe.getPhaseId() == PhaseId.RESTORE_VIEW) {
td.sessionFactory = lookupSessionFactory(pe.getFacesContext());
td.session = null;
td.participate = false;

if (isSingleSession()) {
// single session mode
if (TransactionSynchronizationManager
.hasResource(td.sessionFactory)) {
// Do not modify the Session: just set the participate flag.
td.participate = true;
} else {
td.session = getSession(td.sessionFactory);
TransactionSynchronizationManager.bindResource(
td.sessionFactory, new SessionHolder(td.session));
}
} else {
// deferred close mode
if (SessionFactoryUtils
.isDeferredCloseActive(td.sessionFactory)) {
// Do not modify deferred close: just set the participate
// flag.
td.participate = true;
} else {
SessionFactoryUtils.initDeferredClose(td.sessionFactory);
}
}

}
ts.set(td);
}

@SuppressWarnings("unchecked")
public void afterPhase(PhaseEvent pe) {
if (pe.getPhaseId() == PhaseId.RENDER_RESPONSE) {
ThreadData td = (ThreadData) ts.get();
if (td == null)
return;
if (td.afterTimes != 0) {
td.afterTimes++;
return;
}
td.afterTimes++;
if (!td.participate) {
if (isSingleSession()) {
// single session mode
TransactionSynchronizationManager
.unbindResource(td.sessionFactory);
try {
closeSession(td.session, td.sessionFactory);
} catch (RuntimeException ex) {
ex.printStackTrace();
}
} else {
// deferred close mode
SessionFactoryUtils.processDeferredClose(td.sessionFactory);
}
}
ts.set(null);
}
}

public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}

public static final String DEFAULT_SESSION_FACTORY_BEAN_NAME = "sessionFactory";

private String sessionFactoryBeanName = DEFAULT_SESSION_FACTORY_BEAN_NAME;

private boolean singleSession = true;

/**
* Set the bean name of the SessionFactory to fetch from Spring's root
* application context. Default is "sessionFactory".
*
* @see #DEFAULT_SESSION_FACTORY_BEAN_NAME
*/
public void setSessionFactoryBeanName(String sessionFactoryBeanName) {
this.sessionFactoryBeanName = sessionFactoryBeanName;
}

/**
* Return the bean name of the SessionFactory to fetch from Spring's root
* application context.
*/
protected String getSessionFactoryBeanName() {
return sessionFactoryBeanName;
}

/**
* Set whether to use a single session for each request. Default is "true".
*
* If set to false, each data access operation or transaction will use its
* own session (like without Open Session in View). Each of those sessions
* will be registered for deferred close, though, actually processed at
* request completion.
*
* @see SessionFactoryUtils#initDeferredClose
* @see SessionFactoryUtils#processDeferredClose
*/
public void setSingleSession(boolean singleSession) {
this.singleSession = singleSession;
}

/**
* Return whether to use a single session for each request.
*/
protected boolean isSingleSession() {
return singleSession;
}

/**
* Look up the SessionFactory that this filter should use.
*
* Default implementation looks for a bean with the specified name in
* Spring's root application context.
*
* @return the SessionFactory to use
* @see #getSessionFactoryBeanName
*/
protected SessionFactory lookupSessionFactory(FacesContext facesContext) {
WebApplicationContext wac = FacesContextUtils
.getRequiredWebApplicationContext(facesContext);
return (SessionFactory) wac.getBean(getSessionFactoryBeanName(),
SessionFactory.class);
}

/**
* Get a Session for the SessionFactory that this filter uses. Note that
* this just applies in single session mode!
*
* The default implementation delegates to SessionFactoryUtils' getSession
* method and sets the Session's flushMode to NEVER.
*
* Can be overridden in subclasses for creating a Session with a custom
* entity interceptor or JDBC exception translator.
*
* @param sessionFactory
* the SessionFactory that this filter uses
* @return the Session to use
* @throws DataAccessResourceFailureException
* if the Session could not be created
* @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession(SessionFactory,
* boolean)
* @see org.hibernate.FlushMode#NEVER
*/
@SuppressWarnings("deprecation")
protected Session getSession(SessionFactory sessionFactory)
throws DataAccessResourceFailureException {
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.AUTO);
return session;
}

/**
* Close the given Session. Note that this just applies in single session
* mode!
*
* The default implementation delegates to SessionFactoryUtils'
* releaseSession method.
*
* Can be overridden in subclasses, e.g. for flushing the Session before
* closing it. See class-level javadoc for a discussion of flush handling.
* Note that you should also override getSession accordingly, to set the
* flush mode to something else than NEVER.
*
* @param session
* the Session used for filtering
* @param sessionFactory
* the SessionFactory that this filter uses
*/
protected void closeSession(Session session, SessionFactory sessionFactory) {
SessionFactoryUtils.releaseSession(session, sessionFactory);
}

}


Faces-config.xml registration





After rendering the session will be closed, if you try to retrieve any object from the EntityManager it will give you lazy initialization exception.

If you would like to maintain a single session then you may use the following code snippet to solve your issue











package com.project.util;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.hibernate.FlushMode;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.dao.DataAccessResourceFailureException;

import org.springframework.orm.hibernate3.SessionFactoryUtils;

import org.springframework.orm.hibernate3.support.OpenSessionInViewFilter;

public class SessionFilter extends OpenSessionInViewFilter {

protected final Log logger = LogFactory.getLog(SessionFilter.class);

protected Session getSession(SessionFactory sessionFactory)

throws DataAccessResourceFailureException {

Session session = SessionFactoryUtils.getSession(sessionFactory, true);

session.setFlushMode(FlushMode.AUTO);

logger.info("open session");

return session;

}

protected void closeSession(Session session, SessionFactory factory) {

logger.info("close session");

session.flush();

super.closeSession(session, factory);

}

}



package com.project.util;

import javax.faces.context.FacesContext;

import javax.faces.event.PhaseEvent;

import javax.faces.event.PhaseId;

import javax.faces.event.PhaseListener;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.SessionHolder;

import org.springframework.transaction.support.TransactionSynchronizationManager;

import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.jsf.FacesContextUtils;

public class SessionListner extends SessionFilter implements PhaseListener {

private static final long serialVersionUID = -2080231862377196879L;

private static Log log = LogFactory

.getLog("com.project.util.SessionListner");

public static final String DEFAULT_SESSION_FACTORY_BEAN_NAME = "sessionFactory";

public void beforePhase(PhaseEvent pe) {

if (pe.getPhaseId() == PhaseId.RESTORE_VIEW) {

SessionFactory sessionFactory = lookupSessionFactory(pe

.getFacesContext());

log

.info("Opening single Hibernate Session in SessionListner");

Session session = getSession(sessionFactory);

TransactionSynchronizationManager.bindResource(sessionFactory,

new SessionHolder(session));

}

}

public void afterPhase(PhaseEvent pe) {

SessionFactory sessionFactory = lookupSessionFactory(pe

.getFacesContext());

if (pe.getPhaseId() == PhaseId.RENDER_RESPONSE) {

SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager

.unbindResource(sessionFactory);

log

.info("Closing single Hibernate Session in SessionListner");

closeSession(sessionHolder.getSession(), sessionFactory);

}

}

protected SessionFactory lookupSessionFactory(FacesContext facesContext) {

WebApplicationContext wac = FacesContextUtils

.getRequiredWebApplicationContext(facesContext);

return (SessionFactory) wac.getBean(DEFAULT_SESSION_FACTORY_BEAN_NAME,

SessionFactory.class);

}

public PhaseId getPhaseId() {

return PhaseId.ANY_PHASE;

}

}


Register this listener in your web.xml file



Another work around is




Also we can manage by configuring the hibernate properties in the applicationContext.xml





The solution which I adopted is OpenSessionInViewPhaseListener, it worked for me.