JPA:应用程序必须提供JDBC连接

时间:2015-09-09 04:03:27

标签: java hibernate jpa jdbc

我已经在JBoss 7.1中部署了我的应用程序,数据库连接在JPA persistence.xml中完成。这是配置

<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
            <property name="hibernate.connection.username" value="postgres" />
            <property name="hibernate.connection.password" value="Prashant11" />
            <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>  
            <property name="hibernate.connection.URL"  value="false"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/"/>

引起:在组件ProfileSession上EJB调用失败方法public abstract java.util.List sp.ProfileSessionRemote.getTopProfile(int []):javax.ejb.EJBException:java.lang.UnsupportedOperationException:应用程序必须提供JDBC连接

2 个答案:

答案 0 :(得分:1)

如果您尝试使用独立连接进行连接,即

<persistence-unit name="justshop-pu" transaction-type="RESOURCE_LOCAL">. 

可能是由于:

<property name="hibernate.connection.URL"  value="false"/>

使用

<property name="hibernate.connection.url"  value="false"/> 

代替。区分大小写。

配置数据源:

如果您在持久性单元中使用JTA作为事务类型,并且因为您尝试从作为conatiner托管上下文的EJB进行连接,因此它将默认为JTA事务。您需要配置数据源连接。

这些是JBoss 7.1中用于mysql数据源配置的步骤:

<datasource jndi-name="java:jboss/datasources/ExpenseDS" pool-name="ExpenseDS" enabled="true" use-java-context="true">
                    <connection-url>jdbc:mysql://localhost/expense</connection-url>
                    <driver>com.mysql</driver>
                    <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
                    <pool>
                        <min-pool-size>10</min-pool-size>
                        <max-pool-size>100</max-pool-size>
                        <prefill>true</prefill>
                    </pool>
                    <security>
                        <user-name>username</user-name>
                        <password>pwd</password>
                    </security>
                    <statement>
                        <prepared-statement-cache-size>32</prepared-statement-cache-size>
                        <share-prepared-statements>true</share-prepared-statements>
                    </statement>
                </datasource>
  • 还在standalone.xml中添加驱动程序条目,如下所示:
<driver name="com.mysql" module="com.mysql">
<driver-class>com.mysql.jdbc.Driver</driver-class>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
</driver>
  • 创建一个module.xml文件,如下所示
<?xml version="1.0" encoding="UTF-8"?>

<!--
  ~ JBoss, Home of Professional Open Source.
  ~ Copyright 2010, Red Hat, Inc., and individual contributors
  ~ as indicated by the @author tags. See the copyright.txt file in the
  ~ distribution for a full listing of individual contributors.
  ~
  ~ This is free software; you can redistribute it and/or modify it
  ~ under the terms of the GNU Lesser General Public License as
  ~ published by the Free Software Foundation; either version 2.1 of
  ~ the License, or (at your option) any later version.
  ~
  ~ This software is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
  ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  ~ Lesser General Public License for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public
  ~ License along with this software; if not, write to the Free
  ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  ~ 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  -->

<module xmlns="urn:jboss:module:1.1" name="com.mysql">

    <resources>
        <resource-root path="mysql-connector-java-5.1.31.jar"/>
        <!-- Insert resources here -->
    </resources>
    <dependencies>
        <module name="javax.api"/>
    </dependencies>
</module>
  • 将它与mysql驱动程序jar一起作为模块放入jboss。在jboss中的模块中创建一个模块文件夹:      - 对于ex:对于mysql,因为我们将模块作为com.mysql给出,我们将创建文件夹com / mysql和另一个文件夹main,并将驱动程序类和module.xml放在“com / mysql / main”中(请参阅h2database配置,如果你很困惑)

答案 1 :(得分:-1)

您使用的是哪个版本的Hibernate? 它看起来在hibernate.cfg.xml文件中混合了Hibernate 3.x和4.x语法。

Hibernate 3.x

<properties>
      <property name="hibernate.connection.driver_class" value="...."/>
      <property name="hibernate.connection.url" value="...."/>
      <property name="hibernate.connection.username" value="...."/>
      <property name="hibernate.connection.password" value="...."/>   </properties>

Hibernate 4.x

<properties>
      <property name="javax.persistence.jdbc.driver" value="...."/>
      <property name="javax.persistence.jdbc.url" value="...."/>
      <property name="javax.persistence.jdbc.user" value="...."/>
      <property name="javax.persistence.jdbc.password" value="...."/>
 </properties>