CaptnJunks CaptnJunks - 2 years ago 127
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.


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
<persistence-unit name="testDB" transaction-type="JTA">

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>

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")
public class PersonCreator {

private Person person;

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

public void setPersonValues(){
person = new Person();

public String getPersonValues(){
return "Test";

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

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


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

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


And the Entity Class:

package person;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

* @author jameszeigler
public class Person implements Serializable {

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

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

public void setId(Long 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;

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

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 (( == null && != null) || ( != null && ! {
return false;
return true;

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