【问题标题】:A simple Hibernate test case,but it cannot open connection一个简单的 Hibernate 测试用例,但它无法打开连接
【发布时间】:2016-04-29 22:44:43
【问题描述】:

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC  
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  

<hibernate-configuration>  

<session-factory>  

    <!-- Database connection settings -->  
    <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>  
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernate_first?useUnicode=true&amp;characterEncoding=utf-8</property>   
    <property name="connection.username">root</property>  
    <property name="connection.password">123456</property>  

    <!-- JDBC connection pool (use the built-in) -->  
    <property name="connection.pool_size">1</property>  

    <!-- SQL dialect -->  
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>  

    <!-- Enable Hibernate's automatic session context management -->  
    <property name="current_session_context_class">thread</property>  

    <!-- Disable the second-level cache  -->  
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>  

    <!-- Echo all executed SQL to stdout -->  
    <property name="show_sql">true</property>  

    <!-- Drop and re-create the database schema on startup -->  
    <property name="hbm2ddl.auto">update</property>  

    <mapping resource="com/User.hbm.xml"/>  

</session-factory>  

</hibernate-configuration>  

用户.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>  
   <!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping><!--create user's database-->
    <class name="com.User">
        <id name="id">
            <generator class="uuid"></generator>
        </id>
        <property name="name"></property>
        <property name="password"></property>
        <property name="createTime" type="date"></property>
        <property name="expireTime" type="date"></property>
    </class>

</hibernate-mapping>

错误:

> org.hibernate.exception.JDBCConnectionException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
    at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
    at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
    at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1354)
    at com.Client.main(Client.java:23)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:981)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
    .........

有新的问题...... org.hibernate.exception.JDBCConnectionException:无法打开连接 线程“主”org.hibernate.TransactionException 中的异常:事务未成功启动

【问题讨论】:

  • 删除User.hbm.xml末尾的流浪&lt;/span&gt;...
  • 哦,对不起,这个小错误不是关键,错误依然存在:

标签: java xml hibernate


【解决方案1】:

我终于找到了解决办法:

jdbc:mysql://localhost:3306/hibernate_first?useUnicode=true&characterEncoding=utf-8

应该是:

jdbc:mysql://localhost:3306/hibernate_first?useSSL=true

不建议在没有服务器身份验证的情况下建立 SSL 连接。根据 MySQL 5.5.45+、5.6.26+ 和 5.7.6+ 的要求,如果未设置显式选项,则默认情况下必须建立 SSL 连接。所以我们需要设置useSSL=true;

【讨论】:

  • 我的网址是:db.url=jdbc:mysql://localhost/hsqldb?useConfigs=maxPerformance&characterEncoding=utf8 我应该用 ?useSSL=true 替换 ?useConfigs=maxPerformance&characterEncoding=utf8 吗?因为我在运行 hybris 服务器时遇到了同样的错误
  • 我在路径 [/mcc] 的上下文中为 servlet [zkLoader] 获取 Servlet.service() 引发异常 [处理内部过滤器链时出错。如果我做了上述
猜你喜欢
  • 2017-03-03
  • 2011-03-13
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
  • 2011-03-05
  • 2021-05-18
  • 1970-01-01
相关资源
最近更新 更多