title: Java web project + JPA/JDBC "Error connecting to server localhost on port 1527 with message Connection refused"
body: <p>I have this old web project which is called "Drinks Archive". You can, among other things, add, remove, list and edit drinks. It runs fine locally from NetBeans but when I deploy my project as a .war file to a server, which uses Tomcat 6, it gives the following error every time I try to do a database operation (e.g. add drink to db):</p>
  
  <blockquote>
    <p>javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: java.sql.SQLNonTransientConnectionException: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused.
    Error Code: 40000
        org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:501)
        org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
        org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
        org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:290)
        org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:268)
        archive.Register.getEntityManager(Register.java:53)
        archive.Register.getDrinks(Register.java:170)
        servlets.ListServlet.doGet(ListServlet.java:67)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:717)</p>
  </blockquote>
  
  <p>Here's my source code, (in NetBeans I use GlassFish as a server).</p>
  
  <p>Register.java:</p>
  
  <pre><code>public class Register {
  
  private static Register instance;
  private EntityManager em;
  
  private EntityManagerFactory emf = null;
  
  public static Register getInstance() {
      if (instance == null) {
          instance = new Rekisteri();
      }
  
      return instance;
  }
  
  private Register() {
      try {
          Class.forName("org.apache.derby.jdbc.ClientDriver");
      } catch (ClassNotFoundException ex) {
          Logger.getLogger(Rekisteri.class.getName()).log(Level.SEVERE, null, ex);
      }
      emf = Persistence.createEntityManagerFactory("DrinksArchivePU");
  }
  
  public EntityManager getEntityManager() {
      return emf.createEntityManager();
  }
  
  public void addDrink(Drink new) {
      em = getEntityManager();
      em.getTransaction().begin();
      em.merge(new);
      em.getTransaction().commit();     
  }
  
  etc...//
  }
  </code></pre>
  
  <p>persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.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_2_0.xsd"&gt;
  &lt;persistence-unit name="DrinksArchivePU" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
      &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
          &lt;property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/&gt;
          &lt;property name="eclipselink.jdbc.user" value="d"/&gt; 
          &lt;property name="eclipselink.jdbc.password" value="d"/&gt; 
          &lt;property name="eclipselink.jdbc.url" value="jdbc:derby://localhost:1527/drinks;create=true"/&gt;
          &lt;property name="eclipselink.ddl-generation" value="drop-and-create-tables"/&gt;
          &lt;property name="eclipselink.ddl-generation.output-mode" value="database"/&gt;
          &lt;property name="eclipselink.weaving" value="false"/&gt;
          &lt;property name="eclipselink.logging.level" value="FINE"/&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>
  
  <p>I read that the problem could be because of derby network server not starting on my server but if that's so, how to fix it?</p>
  
  <p><strong>EDIT:</strong> Fixed, I changed to HSQLDB JDBC Driver in my persistence.xml settings and after that it was all about adding different missing jar-files to my project in the server.</p>
  
  <p>Here's persistence.xml now:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.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_2_0.xsd"&gt;
  &lt;persistence-unit name="DrinksAchivePU" transaction-type="RESOURCE_LOCAL"&gt;
  
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
  
      &lt;class&gt;archive.Ingredient&lt;/class&gt;
      &lt;class&gt;archive.Review&lt;/class&gt;
      &lt;class&gt;archive.Drink&lt;/class&gt;
      &lt;class&gt;archive.DrinkCategory&lt;/class&gt;
      &lt;class&gt;archive.User&lt;/class&gt;
      &lt;class&gt;archive.Register&lt;/class&gt;
  
      &lt;properties&gt;
          &lt;property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/&gt;
          &lt;property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb;default_schema=true"/&gt;
          &lt;property name="javax.persistence.jdbc.user" value="sa"/&gt;
          &lt;property name="javax.persistence.jdbc.password" value=""/&gt;
          &lt;property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/&gt;
          &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  </code></pre>
  
  <p></p>