Question

I am using Eclipse Galileo and I wanted to deploy a simple application, using JPA, GlassFish 2.1 and MySQL 5. Unfortunately, I could not find any tutorials for GlassFish 2.1 (just for 3.0, but I cannot use it).

I created a JPA project, added a MySQL5 connection and generated an Entity from the database.

The generate JPA class is:

package model;

import java.io.Serializable;
import javax.persistence.*;

@Entity
@Table(name="customer")
public class Customer implements Serializable {
 private static final long serialVersionUID = 1L;

 @Id
 @Column(name="customer_id")
 private int customerId;

 private String email;

 @Column(name="first_name")
 private String firstName;

 @Column(name="last_name")
 private String lastName;

    public Customer() {
    }

 public int getCustomerId() {
  return this.customerId;
 }

 public void setCustomerId(int customerId) {
  this.customerId = customerId;
 }

 public String getEmail() {
  return this.email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public String getFirstName() {
  return this.firstName;
 }

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

 public String getLastName() {
  return this.lastName;
 }

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

}

And the persistence.xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
 <persistence-unit name="JPAProject2">
  <class>model.Customer</class>
 </persistence-unit>
</persistence>

I created a Dynamic Web Project, and added a new Servlet class, which looks like this:

package servlet;    
import java.io.IOException;    
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;    
import model.Customer;

public class JpaDemoServlet2 extends HttpServlet 
{
 private static final long serialVersionUID = 1L;

 @PersistenceUnit
 private EntityManagerFactory entityManagerFactory;
 @Resource
 private UserTransaction userTransaction;

    public JpaDemoServlet2() 
    {
        super();
    }

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
 {
  EntityManager entityManager =
   entityManagerFactory.createEntityManager();

  Customer customer = new Customer();
  customer.setCustomerId(3);
  customer.setFirstName("Smith");
  customer.setLastName("John");
  customer.setEmail("john.smith@email.com");

  try
  {
   userTransaction.begin();
   entityManager.persist(customer);  
   userTransaction.commit();
  }
  catch(Exception ex)
  {
   response.sendError(1, ex.getMessage());
  }

 }

}

I added in the servlet project's properties Project References and Module Dependencies for the JPA project. Are there any other configuration setting that must be done? So far I was able to publish the Servlet, but unfortunately, I cannot run it. http://localhost:4848/ServletProject2, I get the 'Hello, World!' message, but if I want to access http://localhost:4848/ServletProject2/JpaDemoServlet2 I get this exception:

Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2.1 (Build b60e-fcs (12/23/2008))): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
Error Code: 0

Is there something I am missing?

Was it helpful?

Solution

There are a number of issues I think.

First, the persistence.xml looks a bit odd, I would have expected something like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="JPAProject2" transaction-type="JTA">
    <provider>oracle.toplink.essentials.PersistenceProvider</provider>
    <jta-data-source>jdbc/sample</jta-data-source>
    <class>model.Customer</class>
</persistence-unit>
</persistence>

That is, a provider field, and the necessary fields to indicate that you're running in a server (jta-data-source). Of course, jta-data-source has to refer to a datasource that you configured in Glassfish.

Next, I think it's quite odd that your application runs on ports 4848, normally that's the administrative listener for Glassfish, and I'd expect only the admin console to run there. Did you reconfigure your Glassfish's ports?

One thing that puzzles me is how you got this far with such a configuration: it looks like Toplink thinks it has to contact a Derby running on localhost (port 1527 is standard for Derby) so maybe there's still some other persistence.xml floating around? Please check that.

About tutorials: I use Glassfish a lot, but always with NetBeans. Here are a couple of links to tutorials from the Netbeans site, they might help you.

It may be easiest to just install Netbeans, follow the tutorials and have a look at all the files that get generated, Netbeans automates the creation of a lot of this stuff and I have no idea what degree of assistance Eclipse gives you with these files.

Here is a rather complete tutorial based on Eclipse: http://wiki.eclipse.org/EclipseLink/Examples/JPA/GlassFishV2_Web_Tutorial

A last one: a tutorial for GF3 should get you going on GF2 as well, at least for these technologies (servlet and JPA). OK, GF3 comes with Eclipselink instead of Toplink Essentials, but these two are not that different at all.

Edit: when I saw TLE trying to connect to a Derby on localhost I forgot the part about MySQL. This has been corrected now - references to how you should start Derby have been removed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top