3

我是 glass-fish 和持久性的新手,尝试在我的机器上对 MySQL 数据库运行查询时,出现以下错误:

异常 [EclipseLink-4002](Eclipse Persistence Services - 2.3.2.v20111125-r10461):org.eclipse.persistence.exceptions.DatabaseException 内部异常:java.sql.SQLException:未选择数据库错误代码:1046

在运行服务器之前,我确保使用本指南完成了服务器配置:mysql site manual for using connector with glassfish,并像这样配置了我的 persistence.xml:

<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">
<persistence-unit name="StudentRecipieWebsite" transaction-type="RESOURCE_LOCAL">
    <non-jta-data-source>jdbc/mySql</non-jta-data-source>
    <class>il.musehunter.studentRecipes.dbModel.Image</class>
    <class>il.musehunter.studentRecipes.dbModel.Ingrediant</class>
    <class>il.musehunter.studentRecipes.dbModel.Recipe</class>
    <class>il.musehunter.studentRecipes.dbModel.User</class>
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/recipes_data"/>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="javax.persistence.jdbc.password" value="moshe1475"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
    </properties>
</persistence-unit>

我对此真的很陌生,并且不知道我接下来应该做什么,因此非常感谢任何帮助。

编辑:

从我的 glassfish 服务器添加 domain.xml 的一部分:

<applications>
<application context-root="/StudentRecipieWebsite" location="${com.sun.aas.instanceRootURI}/applications/StudentRecipieWebsite/" name="StudentRecipieWebsite" object-type="user">
  <property name="appLocation" value="${com.sun.aas.instanceRootURI}/applications/__internal/StudentRecipieWebsite/StudentRecipieWebsite.war"></property>
  <property name="org.glassfish.ejb.container.application_unique_id" value="89812698949156864"></property>
  <property name="org.glassfish.persistence.app_name_property" value="StudentRecipieWebsite"></property>
  <property name="defaultAppName" value="StudentRecipieWebsite"></property>
  <module name="StudentRecipieWebsite">
    <engine sniffer="ejb"></engine>
    <engine sniffer="security"></engine>
    <engine sniffer="jpa"></engine>
    <engine sniffer="web"></engine>
  </module>
</application>

和:

<resources>
<jdbc-resource pool-name="__TimerPool" jndi-name="jdbc/__TimerPool" object-type="system-admin"></jdbc-resource>
<jdbc-resource pool-name="DerbyPool" jndi-name="jdbc/__default"></jdbc-resource>
<jdbc-connection-pool datasource-classname="org.apache.derby.jdbc.EmbeddedXADataSource" res-type="javax.sql.XADataSource" name="__TimerPool">
  <property name="databaseName" value="${com.sun.aas.instanceRoot}/lib/databases/ejbtimer"></property>
  <property name="connectionAttributes" value=";create=true"></property>
</jdbc-connection-pool>
<jdbc-connection-pool is-isolation-level-guaranteed="false" datasource-classname="org.apache.derby.jdbc.ClientDataSource" res-type="javax.sql.DataSource" name="DerbyPool">
  <property name="PortNumber" value="1527"></property>
  <property name="Password" value="APP"></property>
  <property name="User" value="APP"></property>
  <property name="serverName" value="localhost"></property>
  <property name="DatabaseName" value="sun-appserv-samples"></property>
  <property name="connectionAttributes" value=";create=true"></property>
</jdbc-connection-pool>
<jdbc-connection-pool datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" res-type="javax.sql.DataSource" name="MySQLConnPool">
  <property name="portNumber" value="3306"></property>
  <property name="databaseName" value="recipes_data"></property>
  <property name="serverName" value="localhost"></property>
  <property name="user" value="root"></property>
  <property name="password" value="moshe1475"></property>
  <property name="URL" value="jdbc:mysql://:3306/"></property>
</jdbc-connection-pool>
<jdbc-resource pool-name="MySQLConnPool" description="" jndi-name="jdbc/mySql"></jdbc-resource></resources>
4

1 回答 1

5

我的第一个猜测是数据源定义中的连接 URL 不太正确,因为“No database selected Error Code: 1046”是 MySQL 错误。

persistence.xml如果您已经在应用服务器中定义了数据源(这是常用的方式),则不需要配置数据源。您可以发布您的数据源定义$GLASSFISH_HOME/glassfish/domains/$GLASSFISH_DOMAIN/config/domain.xml吗?

这是我从我的一个项目(或多或少来自 Netbeans 模板)中获取的一个示例 persistence.xml,它可能对您有所帮助:

<?xml version="1.0" encoding="UTF-8"?>
<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">

  <persistence-unit name="MyPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/sample</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
       <property name="eclipselink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

祝你好运!

回复评论:

您需要将数据库名称添加到 URL:

<property name="URL" value="jdbc:mysql://nameOrIPOfYourServerOrLocalhost:3306/nameOfYourDatabase"></property>

那应该可以解决问题。

于 2013-06-05T09:57:41.603 回答