javaeclipsejpaeclipselinkconnectexception

Java JPA EclipseLink databaseException


Trying to get a simple JPA application running using eclipselink. I have persistence.xml stored in src/META-INF/ and have imported "eclipselink-2.3.0.jar", "javax.persistence.jar" and "mysql-connector-java-5.0.4-bin.jar" to the project (using Eclipse IDE)

Server.java

public class Server {
private EntityManagerFactory emFactory;

Server() {
    super();
    emFactory =  Persistence.createEntityManagerFactory("test");
}

public void newAccount(String name){
    EntityManager em = null;
    try {
        em = beginTransaction();
        List<Account> 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");
}
}

Account.java

@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;
}
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         version="2.0">

<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <provider>
       org.eclipse.persistence.jpa.PersistenceProvider
    </provider>

    <class>Account</class>

      <properties>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    <property name="eclipselink.ddl-generation.output-mode" value="database"/>
        <property name="eclipselink.logging.level" value="INFO"/>
        <property name="javax.persistence.jdbc.password" value="javajava"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/bank"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
    </properties>
</persistence-unit>
</persistence>

output

[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.<init>(MysqlIO.java:276)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
    at com.mysql.jdbc.Connection.<init>(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.<init>(MysqlIO.java:276)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
    at com.mysql.jdbc.Connection.<init>(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.<init>(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)

No idea what's the problem other than that something in the beginTranscation() method is failing (returns null) for some reason.


Solution

  • MESSAGE: java.net.ConnectException: Connection refused: connect

    Either mysql is not running, or it is not using the default port (3306).

    Try checking the connections settings, connect manually with a tool like mysql workbench.

    This is the setting you may need to change if the mysql port is different than the default:

    property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/bank"