当前位置:   article > 正文

使用JPA persistence.xml报错:A ResourcePool could not acquire a resource from its primary factory or sour_oracle jdbcconnectionexception: unable to acquire

oracle jdbcconnectionexception: unable to acquire jdbc connection

使用JPA persistence.xml 配置时,报错:

Unable to acquire JDBC Connection

Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!

Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.

首先检查persistence.xml中url,driver,username,password的写法是否正确,我是直接把hibernate.cfg.xml下面的拷贝过来的,所以写错了,对比如下:

hibernate:

  1. <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
  2. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  3. <property name="connection.username">root</property>
  4. <property name="connection.password">123456</property>

JPA:

  1. <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hibernate"/>
  2. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  3. <property name="javax.persistence.jdbc.user" value="root"/>
  4. <property name="javax.persistence.jdbc.password" value="123456"/>

注意driver_class 变为driver,username变为user,还有前缀也要变为 javax.persistence.jdbc,最后附上正确的persistence.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  5. http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
  6. version="2.2">
  7. <!--transaction-type:事务类型-->
  8. <!--JTA:JAVA Transaction API-->
  9. <!--RESOURCE_LOCAL:本地代码事务-->
  10. <persistence-unit name="myJPAUnit" transaction-type="RESOURCE_LOCAL">
  11. <!--JPA提供商,可以不写-->
  12. <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
  13. <!--JPA注解的实体类位置,可以不写-->
  14. <class>domain.CstCustomer</class>
  15. <class>domain.CstLinkman</class>
  16. <!--以下只要把cfg.xml文件中的拷贝过来稍加改动即可-->
  17. <!--1.连接数据库的信息-->
  18. <properties>
  19. <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/hibernate"/>
  20. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  21. <property name="javax.persistence.jdbc.user" value="root"/>
  22. <property name="javax.persistence.jdbc.password" value="123456"/>
  23. <!--2.hibernate可选配置-->
  24. <!--检测实体类的映射配置和数据库的表结构是否一致,如果不一致,更新表结构-->
  25. <property name="hbm2ddl.auto" value="update"/>
  26. <!--数据库方言-->
  27. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
  28. <!--是否显示hibernate生成的sql语句-->
  29. <property name="hibernate.show_sql" value="true"/>
  30. <!--是否使用格式化输出sql语句-->
  31. <property name="hibernate.format_sql" value="true"/>
  32. <!--3.设置hibernate的连接池提供商-->
  33. <property name="hibernate.connection.provider_class" value="org.hibernate.c3p0.internal.C3P0ConnectionProvider"/>
  34. </properties>
  35. </persistence-unit>
  36. </persistence>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/627471
推荐阅读
相关标签
  

闽ICP备14008679号