如何从Glassfish连接到外部MySQL服务器

时间:2011-10-17 13:57:53

标签: java mysql java-ee glassfish

我正在尝试连接到远程MySQL服务器。我已经成功地从Eclipse(DataSource视图)建立了连接,现在可以浏览数据库的内容。

但是现在我希望能够在我的Java EE 6应用程序中使用该数据库,但它不起作用。我尝试过:

在管理面板中添加了用户名,密码,服务器名称,URL,端口作为属性。

这是我使用的URL:jdbc:mysql:// MYHOST:3306 / MYDATABASE可能没什么问题。我还将我的IP添加到CPanel中的远程列表中,因此我可以远程连接。我已下载此驱动程序:http://www.java.net/external?url=http://dev.mysql.com/downloads/connector/j/并将其添加到domains / domain1 / lib文件夹中。

我还看到其他4个文件夹;数据库,ext,applibs和类,所以我将驱动程序添加到这些文件夹中以确保。这是错的吗?我是否需要将驱动程序添加到Eclipse项目中的某个文件夹中才能使其正常工作?

资源类型:javax.sql.DataSource 数据源类名:com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource

更新

  

HTTP状态500 -

     

输入例外报告

     

消息

     

description服务器遇到阻止的内部错误()   它来自履行这一要求。

     

例外

     

javax.servlet.ServletException:WELD-000049无法调用[方法]   @PostConstruct public   no.breakpoint.sertifikat.interfaces.QuestionController.intialize()on   no.breakpoint.sertifikat.interfaces.QuestionController@10614f3d

     

根本原因

     

org.jboss.weld.exceptions.WeldException:WELD-000049无法调用   [方法] @PostConstruct public   no.breakpoint.sertifikat.interfaces.QuestionController.intialize()on   no.breakpoint.sertifikat.interfaces.QuestionController@10614f3d

     

根本原因

     

java.lang.reflect.InvocationTargetException

     

根本原因

     

javax.ejb.EJBException异常

     

根本原因

     

显示java.lang.NullPointerException

     

注意异常的完整堆栈跟踪及其根本原因是   可在GlassFish Server Open Source Edition 3.1.1日志中找到。   GlassFish Server开源版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;
    }
}

错误日志:

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();
    }
}

如果我取消注释我现在注释的数据表我使用的工作,500错误就消失了。但是当我试图从数据库中获取它时,它给了我那个。

1 个答案:

答案 0 :(得分:2)

  

引起:java.lang.NullPointerException       at no.breakpoint.sertifikat.application.QuestionManagerEJB.all(QuestionManagerEJB.java:52)

那么,EntityManagernull@PersistenceContext只会注入容器/框架托管类,例如@Stateless EJB,CDI @Named bean,JSF @ManagedBean bean等。{{1}但似乎根本没有管理。

鉴于目前的设置,我开始将DefaultQuestionDao标记为@Stateless EJB。整个设置最让人感到困惑,堆栈跟踪告诉我DefaultQuestionDao我在代码片段中没有看到,只有QuestionManagerEJBDefaultQuestionDao。也许您需要在QuestionBusinessLocal中注入DefaultQuestionDao @EJB

此外,还有另一个问题:@NamedQuery必须在@Entity类或其中一个超类上注释,否则将无法找到它。