2013-02-03 152 views
0

我使用eclipse有ollowing的persistence.xml在我的JSF項目Glassfish的@PersistenceContext

import java.util.Date; 

import org.englishonline.persistence.*; 
import javax.faces.bean.*; 
import javax.persistence.*; 

@ManagedBean 
@RequestScoped 
public class Login {  

    @PersistenceContext(unitName="EnglishOnline") 
    private EntityManager em; 
    private String login = null; 
    private String password = null; 

    public String getLogin() { 
     return login; 
    } 
    public void setLogin(String login) { 
     this.login = login; 
    } 
    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 

    public String performLogin() { 
     if (login.equals(password)) { 

      EntityTransaction tx = em.getTransaction(); 
      User user = new User(); 
      user.setBirthdate(new Date()); 
      user.setLogin(login); 
      user.setPassword(password); 
      user.setName("Bobby"); 
      user.setRegistrationdate(new Date()); 
      user.setSex(Sex.MALE); 
      tx.begin(); 
      em.persist(user); 
      tx.commit(); 

      return null;    
     } else { 
      return null; 
     } 

    } 

} 

但是,當我嘗試在我的應用程序提交按鈕,服務器日誌給出了這樣:

WARNING: RAR5117 : Failed to obtain/create connection from connection pool [ DerbyPool ]. Reason : com.sun.appserv.connectors.internal.api.PoolingException: Connection could not be allocated because: java.net.ConnectException : Connection to localhost port 1527 Connection refused: connect. 
    WARNING: RAR5114 : Error allocating connection : [Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Connection refused: connect.] 
    SEVERE: Local Exception Stack: 
    Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException 
    Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Connection refused: connect. 
    Error Code: 0 
     at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:309) 
     at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:135) 
     at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162) 
     at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:582) 
     at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:206) 
     at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:472) 
     at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188) 
     at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277) 
     at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:290) 
     at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:275) 
     at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:218) 
     at com.sun.enterprise.container.common.impl.EntityManagerWrapper.getTransaction(EntityManagerWrapper.java:857) 
     at org.englishonline.backing.Login.performLogin(Login.java:34) 
     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.el.parser.AstValue.invoke(AstValue.java:234) 
     at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297) 
     at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
     at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) 
     at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
     at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
     at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) 
     at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) 
     at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
     at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
     at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
     at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
     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.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Ошибка соединения с сервером localhost на порту 1527 с сообщением Connection refused: connect. 
     at com.sun.gjc.spi.base.DataSource.getConnection(DataSource.java:151) 
     at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:132) 
     ... 52 more 
    Caused by: javax.resource.spi.ResourceAllocationException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Ошибка соединения с сервером localhost на порту 1527 с сообщением Connection refused: connect. 
     at com.sun.enterprise.connectors.ConnectionManagerImpl.internalGetConnection(ConnectionManagerImpl.java:307) 
     at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:190) 
     at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:165) 
     at com.sun.enterprise.connectors.ConnectionManagerImpl.allocateConnection(ConnectionManagerImpl.java:160) 
     at com.sun.gjc.spi.base.DataSource.getConnection(DataSource.java:145) 
     ... 53 more 

而且JPA提供商什麼都不做。

+0

您的錯誤讓我想起了。 http://stackoverflow.com/questions/10420902/data-sources-derby-connection-refused – MrSimpleMind

+0

至於你的例子 - 端口1527是德比的本地端口。 +我的服務器正在定義運行和數據庫。 –

回答

4

您將事務類型指定爲需要JTA數據源的JTA。如果您希望指定jdbc.url並直接控制事務而不是使用JTA,則需要使用「resource_local」。

相關問題