CaptnJunks CaptnJunks - 1 year ago 89
Java Question

ValidationException Exception Description: Cannot acquire data source [jdbc/MyDataBase]

I've googled like crazy, but I am not locating what is going wrong using java persistence.

I am using glassfish 4.1.1. NetBeans 8.1. Glassfish is integrated with netbeans and the Server and JDBC resources all show up in the Windows->Services area.

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 entry for the Connection Pool and Resource

<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>


The Pool pings fine from the Glassfish Admin GUI, so I am pretty sure it's there.

My Java Objects:
Class with Entity Manager:

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

}


And the Entity Class:

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 + " ]";
}

}


I swear this should work but I get the exception in testing:

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]


The server has this as a resource, am I doing this wrong? I thought I might need to establish a connection rather than creating an Entity Manager? There is not another server running currently. I feel like I am missing something very simple, but I am not sure what it is.

Answer Source

Had to come back to this one. I seemed to have solved the issue by switching from Galssfish 4.1 over to Payara. Glassfish 4.1 apparently has issues with JDBC resources and Connection Pools. AND I totally incorrectly configured my NetBeans. NetBeans comes with its own GlassFish installation so Instead of using the GlassFish server I had installed and Configured, it was using its own and had no idea what jdbc pool I was talking about.

I was able to figure that out after getting errors of shutting down the GlassFish domains and getting errors of Ports being in-use. I finally realized I had two running instances of the Server. The one I downloaded and installed, and the one NetBeans was running as well.

It was a good learning experience to make sure I configured NetBeans to either use the installed server for local testing ( easier ) or make sure any configurations I make in my local domain.xml that I copied over into my NetBeans install ( less easy ).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download