0

eclipselink を使用して単純な JPA アプリケーションを実行しようとしています。persistence.xmlsrc/META-INF/に保存し、「eclipselink-2.3.0.jar」「javax.persistence.jar」、および「mysql-connector-java-5.0.4-bin.jar」をインポートしましたプロジェクト (Eclipse IDE を使用)

サーバー.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>

出力

[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)

beginTranscation() メソッドの何かが何らかの理由で失敗している (null を返す) こと以外に、何が問題なのかわかりません。

4

1 に答える 1

3

MESSAGE: java.net.ConnectException: Connection refused:接続

mysqlが実行されていないか、デフォルト ポート (3306) を使用していません。

接続設定を確認して、mysqlワークベンチなどのツールで手動で接続してみてください。

これは、mysql ポートがデフォルトと異なる場合に変更する必要がある設定です。

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

于 2013-12-19T16:07:32.043 に答える