开发者

How to connect to external MySQL server from Glassfish

I am trying to connect to a remote MySQL server. I have succesfully made a connection from Eclipse (DataSource view) and can now browse the content of the database.

But now I want to be able to use that database in my Java EE 6 application and it does not work. What I have tried:

Added the开发者_开发知识库 username, password, servername, URL, Port as properties in the administration panel.

This is the URL I use: jdbc:mysql://MYHOST:3306/MYDATABASE probably nothing wrong with it. I have also added the my IP to the remote list in CPanel so I am allowed to connect remotely. I have downloaded this driver: http://www.java.net/external?url=http://dev.mysql.com/downloads/connector/j/ and added it to the domains/domain1/lib folder.

I also see 4 other folders there; databases, ext, applibs and classes so I added the driver to in those folders to just to be sure. Is this wrong? Do I need to add the driver to some folder in my Eclipse project in order to get it to work?

Resource Type: javax.sql.DataSource Datasource classname: com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource

UPDATE

HTTP Status 500 -

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: WELD-000049 Unable to invoke [method] @PostConstruct public no.breakpoint.sertifikat.interfaces.QuestionController.intialize() on no.breakpoint.sertifikat.interfaces.QuestionController@10614f3d

root cause

org.jboss.weld.exceptions.WeldException: WELD-000049 Unable to invoke [method] @PostConstruct public no.breakpoint.sertifikat.interfaces.QuestionController.intialize() on no.breakpoint.sertifikat.interfaces.QuestionController@10614f3d

root cause

java.lang.reflect.InvocationTargetException

root cause

javax.ejb.EJBException

root cause

java.lang.NullPointerException

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.1 logs. GlassFish Server Open Source Edition 3.1.1

EJB:

    @Override
    public List<Question> all() {

//      List<Question> list = new ArrayList<Question>();
//      Question question = new Question();
//      question.setDescription("SDfasfasdfasf");
//      question.setId(32);
//      question.setImage("sdafasdf");
//      list.add(question);
//      
//      return list;
//      
        return questionDao.all();

    }

JSF bean

@Named
@RequestScoped
public class QuestionController {

    @EJB
    private QuestionLocalBusiness questionManager;

    private List<Question> questions;

    @PostConstruct
    public void intialize() {
        questions = questionManager.all();      
    }

    public void setQuestions(List<Question> list) {
        questions = list;
    }

    public List<Question> getQuestions() {
        return questions;
    }
}

Error log:

WARNING: StandardWrapperValve[FacesServlet]: PWC1406: Servlet.service() for servlet FacesServlet threw exception
org.jboss.weld.exceptions.WeldException: WELD-000049 Unable to invoke [method] @PostConstruct public com.bpoint.interfaces.QuestionController.intialize() on com.bpoint.interfaces.QuestionController@10614f3d
    at org.jboss.weld.bean.AbstractClassBean.defaultPostConstruct(AbstractClassBean.java:595)
    at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget.postConstruct(ManagedBean.java:200)
    at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:340)
    at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:121)
    at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:99)
    at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:87)
    at no.breakpoint.sertifikat.interfaces.org$jboss$weld$bean-SertifikatWeb_war-ManagedBean-class_no$breakpoint$sertifikat$interfaces$QuestionController_$$_WeldClientProxy.getQuestions(org$jboss$weld$bean-SertifikatWeb_war-ManagedBean-class_no$breakpoint$sertifikat$interfaces$QuestionController_$$_WeldClientProxy.java)
    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:597)
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:302)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:116)
    at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:55)
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
    at javax.faces.component.UIData.getValue(UIData.java:731)
    at javax.faces.component.UIData.getDataModel(UIData.java:1798)
    at javax.faces.component.UIData.getRowCount(UIData.java:356)
    at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:456)
    at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:201)
    at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:180)
    at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:85)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1763)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1756)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:401)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.reflect.InvocationTargetException  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:597)
    at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:305)
    at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:54)
    at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:163)
    at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:299)
    at org.jboss.weld.introspector.jlr.WeldMethodImpl.invoke(WeldMethodImpl.java:193)
    at org.jboss.weld.bean.AbstractClassBean.defaultPostConstruct(AbstractClassBean.java:591)
    ... 66 more
Caused by: javax.ejb.EJBException
    at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5193)
    at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5091)
    at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4879)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2039)
    at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1990)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:222)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
    at $Proxy231.all(Unknown Source)
    at no.breakpoint.sertifikat.interfaces.QuestionController.intialize(QuestionController.java:24)
    ... 76 more
Caused by: java.lang.NullPointerException
    at no.breakpoint.sertifikat.application.QuestionManagerEJB.all(QuestionManagerEJB.java:52)
    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:597)
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1052)
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1124)
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:5366)
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:619)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:571)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doAround(SystemInterceptorProxy.java:162)
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:144)
    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:597)
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:861)
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:800)
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:370)
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:5338)
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:5326)
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:214)
    ... 79 more

DAO:

package no.breakpoint.sertifikat.infrastructure;

import java.util.List;

@NamedQuery(name = "allQuestions", query = "SELECT q FROM Question q")
public class DefaultQuestionDao implements QuestionDao {

    @PersistenceContext(unitName = "MyDatabasePU")
    private EntityManager entityManager;

    @Override
    public void create(Question question) {
        entityManager.persist(question);
    }

    @Override
    public void delete(Question question) {
        entityManager.remove(question);
    }

    @Override
    public void update(Question question) {
        entityManager.merge(question);
    }

    @Override
    public Question read(Integer id) {
        return entityManager.find(Question.class, id);
    }

    @Override
    public List<Question> all() {
        TypedQuery<Question> query = entityManager.createNamedQuery("allQuestions", Question.class);
        return query.getResultList();
    }
}

If I uncomment what I have commented now the dataTable I use works and the 500 error goes away. But when I try to get it from the database it gives me that.


Caused by: java.lang.NullPointerException at no.breakpoint.sertifikat.application.QuestionManagerEJB.all(QuestionManagerEJB.java:52)

So, the EntityManager is null? The @PersistenceContext will only be injected in container/framework managed classes such as a @Stateless EJB, a CDI @Named bean, a JSF @ManagedBean bean, etc. Your DefaultQuestionDao however doesn't seem to be managed at all.

Given the setup so far, I'd start marking your DefaultQuestionDao as a @Stateless EJB. The whole setup is by the way confusing at best, the stacktrace tells about QuestionManagerEJB which I don't see back in the code snippets, only a DefaultQuestionDao and a QuestionBusinessLocal. Perhaps you need to inject the DefaultQuestionDao as @EJB in your QuestionManagerEJB as well.

Further, there's another problem: that @NamedQuery has got to be annotated on an @Entity class or one of its superclasses, or it won't be found.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜