ValidationException异常描述:无法获取数据源[jdbc / MyDataBase]

时间:2016-08-04 19:55:00

标签: java jdbc netbeans glassfish

我用谷歌搜索过疯狂,但我找不到使用java持久性的错误。

我正在使用glassfish 4.1.1。 NetBeans 8.1。 Glassfish与netbeans集成,服务器和JDBC资源都显示在Windows->服务区域。

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="testDB" transaction-type="JTA">       
       <jta-data-source>jdbc/MyDataBase</jta-data-source>
       <class>person.Person</class>
       <exclude-unlisted-classes>false</exclude-unlisted-classes>   
  </persistence-unit>
</persistence>

连接池和资源的GlassFish domain.xml条目

<jdbc-resource pool-name="MyDataBase" object-type="system-all" jndi-name="jdbc/MyDataBase"></jdbc-resource>
<jdbc-connection-pool driver-classname="java.sql.Driver" ping="true" datasource-classname="org.postgresql.Driver" name="MyDataBase" res-type="java.sql.Driver">
      <property name="URL" value="jdbc:postgresql://localhost:5432/testDB"></property>
      <property name="connectionAttributes" value=";create=true"></property>
      <property name="user" value="*****"></property>
      <property name="password" value="****"></property>
      <property name="portNumber" value="5432"></property>
      <property name="databaseName" value="testDB"></property>
      <property name="serverName" value="localhost"></property>
</jdbc-connection-pool>

Pool从Glassfish管理GUI中读得很好,所以我很确定它在那里。

我的Java对象: 具有实体管理器的类:

package person;

import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author jameszeigler
 */
@Named(value = "personCreator")
@RequestScoped
public class PersonCreator {

     private Person person;

     /**
      * Creates a new instance of PersonCreator
      */
     public PersonCreator() {
     }

     public void setPersonValues(){
          person = new Person();
          person.setAge(10);
          person.setFirstName("James");
          person.setLastName("Zeigler");
     }

     public String getPersonValues(){
          return "Test";
     }

     public void persistPerson(){
      EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "testDB" );

      EntityManager entitymanager = emfactory.createEntityManager( );
      entitymanager.getTransaction( ).begin( );

      setPersonValues();

      entitymanager.persist( person );
      entitymanager.getTransaction( ).commit( );

      entitymanager.close( );
      emfactory.close( );
     }

}

实体类:

package person;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 *
 * @author jameszeigler
 */
@Entity
public class Person implements Serializable {

     // Data Members //
     private static final long serialVersionUID = 1L;
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     private Long id;
     @Column(name="firstName")
     private String firstName;
     @Column(name="lastName")
     private String lastName;
     @Column(name="age")
     private int age;


     // Methods //
     public Long getId() {
          return id;
     }

     public void setId(Long id) {
          this.id = id;
     }

     public void setFirstName(String firstName){
          this.firstName = firstName;
     }

     public String getFirstName(){
          return firstName;
     }

     public void setLastName(String lastName){
          this.lastName = lastName;
     }

     public String getLastName(){
          return lastName;
     }

     public void setAge(int age){
          this.age = age;
     }

     public int getAge(){
          return age;
     }

     @Override
     public int hashCode() {
          int hash = 0;
          hash += (id != null ? id.hashCode() : 0);
          return hash;
     }

     @Override
     public boolean equals(Object object) {
          // TODO: Warning - this method won't work in the case the id fields are not set
          if (!(object instanceof Person)) {
               return false;
          }
          Person other = (Person) object;
          if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
               return false;
          }
          return true;
     }

     @Override
     public String toString() {
          return "person.Person[ id=" + id + " ]";
     }

}

我发誓这应该有用但我在测试中得到了例外:

    javax.persistence.PersistenceException: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.6.1.v20150605-31e8258): 
org.eclipse.persistence.exceptions.ValidationException Exception Description: 
Cannot acquire data source [jdbc/MyDataBase]. Internal Exception: javax.naming.NamingException: Lookup failed for 'jdbc/MyDataBase' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory,
 java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: MyDataBase not found]

服务器将此作为资源,我这样做错了吗?我以为我可能需要建立连接而不是创建实体管理器?目前没有其他服务器在运行。我觉得我错过了很简单的东西,但我不确定它是什么。

1 个答案:

答案 0 :(得分:1)

不得不回到这一个。我似乎已经从Galssfish 4.1转换到Payara解决了这个问题。 Glassfish 4.1显然存在JDBC资源和连接池的问题。我完全错误地配置了我的NetBeans。 NetBeans附带了自己的GlassFish安装,所以不使用我安装和配置的GlassFish服务器,而是使用它自己,并且不知道我在说什么jdbc池。

在收到关闭GlassFish域的错误并且正在使用Ports的错误之后,我能够解决这个问题。我终于意识到我有两个正在运行的服务器实例。我下载并安装的那个,以及一个NetBeans也正在运行。

确保我将NetBeans配置为使用已安装的服务器进行本地测试(更简单)或确保我在本地domain.xml中进行的任何配置(我将其复制到NetBeans安装中),这是一次很好的学习体验。容易)。