【问题标题】:persistence exception on database connection using glassfish使用 glassfish 进行数据库连接的持久性异常
【发布时间】:2013-06-04 16:15:57
【问题描述】:

我是 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>

【问题讨论】:

    标签: mysql glassfish persistence


    【解决方案1】:

    我的第一个猜测是数据源定义中的连接 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>
    

    这应该可以解决问题。

    【讨论】:

    • 嗨,詹斯,谢谢你帮助我!我添加了一些认为相关的 domain.xml 文件(整个文件非常大)。我对配置持久性和glassfish一无所知,只做了我在教程中缝制的东西,所以你给我的这个xml sn-p对我没有帮助,因为我不知道我应该添加哪些属性......我所知道的是我已经坚持了一个星期,所以任何帮助都会非常感激
    • 在我的回答中添加了建议。
    • 终于!! :) 你是正确的,除了将属性添加为:jdbc:mysql://nameOrIPOfYourServerOrLocalhost:3306/nameOfYourDatabase 我必须添加没有 jdbc 前缀的 url,如下所示:nameOrIPOfYourServerOrLocalhost:3306/nameOfYourDatabase 并且它有效!非常感谢
    • 仅供参考,我的 Glassfish 3.1 管理控制台有两个不同的属性“Url”和“URL”。无法想象为什么这是有道理的,但我遇到了这个问题,因为我只更改了第一个以包含数据库名称,并且必须同时更改它们。
    猜你喜欢
    • 1970-01-01
    • 2020-11-25
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 2015-06-09
    相关资源
    最近更新 更多