title: Java JPA EclipseLink databaseException
body: <p>Trying to get a simple JPA application running using eclipselink. I have <strong>persistence.xml</strong> stored in <strong>src/META-INF/</strong> and have imported <em>"eclipselink-2.3.0.jar"</em>, <em>"javax.persistence.jar"</em> and <em>"mysql-connector-java-5.0.4-bin.jar"</em> to the project (using Eclipse IDE)</p>
  
  <p>Server.java</p>
  
  <pre><code>public class Server {
  private EntityManagerFactory emFactory;
  
  Server() {
      super();
      emFactory =  Persistence.createEntityManagerFactory("test");
  }
  
  public void newAccount(String name){
      EntityManager em = null;
      try {
          em = beginTransaction();
          List&lt;Account&gt; existingAccounts = em.createNamedQuery("findAccountWithName", Account.class).setParameter("ownerName", name).getResultList();
          if (existingAccounts.size() != 0) {
              System.out.println("already exists"); return;
          }
  
          Account account = new Account(name, 5000);
          em.persist(account);
          System.out.println("Account " + name + " created");
      } finally { commitTransaction(em); }
  }
  
  private EntityManager beginTransaction() {
      EntityManager em = emFactory.createEntityManager();
      EntityTransaction transaction = em.getTransaction();
      transaction.begin();
      return em;
  }
  
  private void commitTransaction(EntityManager em) { em.getTransaction().commit(); }  
  
      public static void main(String[] args) {
      Server k = new Server();
      k.newAccount("Bob");
  }
  }
  </code></pre>
  
  <p>Account.java</p>
  
  <pre><code>@NamedQuery(
  name = "findAccountWithName",
  query = "SELECT acct FROM Account acct WHERE acct.owner LIKE :ownerName",
  lockMode = LockModeType.OPTIMISTIC 
  )
  })
  
  @Entity(name = "Account")
  public class Account implements Serializable {
  private static final long serialVersionUID = 1L;
  
  @Id
  @Column(name = "id", nullable = false)
  @GeneratedValue(strategy = GenerationType.SEQUENCE)
  long id;
  
  @Column(name = "balance", nullable = false)
  int balance;
  
  @Column(name = "owner", nullable = false)
  String owner;
  
  @Version
  @Column(name="OPTLOCK")
  int versionNum;
  
  public Account(String owner, int balance) {
      this.owner = owner;
      this.balance = balance;
  }
  }
  </code></pre>
  
  <p>persistence.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           version="2.0"&gt;
  
  &lt;persistence-unit name="test" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;provider&gt;
         org.eclipse.persistence.jpa.PersistenceProvider
      &lt;/provider&gt;
  
      &lt;class&gt;Account&lt;/class&gt;
  
        &lt;properties&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.logging.level" value="INFO"/&gt;
          &lt;property name="javax.persistence.jdbc.password" value="javajava"/&gt;
          &lt;property name="javax.persistence.jdbc.user" value="root"/&gt;
          &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/bank"/&gt;
          &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt;
      &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>output</p>
  
  <pre><code>[EL Info]: 2013-12-19 16:32:36.467--ServerSession(1685940940)--EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
  [EL Severe]: 2013-12-19 16:32:40.568--ServerSession(1685940940)--Local Exception Stack: 
  Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
  Internal Exception: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 
  
  ** BEGIN NESTED EXCEPTION ** 
  
  java.net.SocketException
  MESSAGE: java.net.ConnectException: Connection refused: connect
  
  STACKTRACE:
  
  java.net.SocketException: java.net.ConnectException: Connection refused: connect
      at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:276)
      at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
      at com.mysql.jdbc.Connection.&lt;init&gt;(Connection.java:1531)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:98)
      at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
      at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:582)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:206)
      at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:472)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:290)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:268)
      at Server.beginTransaction(Server.java:84)
      at Server.newAccount(Server.java:38)
      at Server.main(Server.java:30)
  
  
  ** END NESTED EXCEPTION **
  
  
  
  Last packet sent to the server was 18 ms ago.
  Error Code: 0
      at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
      at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:319)
      at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:138)
      at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
      at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:582)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:206)
      at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:472)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:290)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:268)
      at Server.beginTransaction(Server.java:84)
      at Server.newAccount(Server.java:38)
      at Server.main(Server.java:30)
  Caused by: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 
  
  ** BEGIN NESTED EXCEPTION ** 
  
  java.net.SocketException
  MESSAGE: java.net.ConnectException: Connection refused: connect
  
  STACKTRACE:
  
  java.net.SocketException: java.net.ConnectException: Connection refused: connect
      at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:276)
      at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
      at com.mysql.jdbc.Connection.&lt;init&gt;(Connection.java:1531)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:98)
      at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
      at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:582)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:206)
      at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:472)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:290)
      at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:268)
      at Server.beginTransaction(Server.java:84)
      at Server.newAccount(Server.java:38)
      at Server.main(Server.java:30)
  
  
  ** END NESTED EXCEPTION **
  
  
  
  Last packet sent to the server was 18 ms ago.
      at com.mysql.jdbc.Connection.createNewIO(Connection.java:2741)
      at com.mysql.jdbc.Connection.&lt;init&gt;(Connection.java:1531)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:98)
      ... 11 more
  
  Exception in thread "main" java.lang.NullPointerException
      at Server.commitTransaction(Server.java:90)
      at Server.newAccount(Server.java:47)
      at Server.main(Server.java:30)
  </code></pre>
  
  <p>No idea what's the problem other than that something in the beginTranscation() method is failing (returns null) for some reason.</p>