【问题标题】:Are there any settings within hibernate/spring jpa to cope with connection failure and reconnection with Hibernate and Spring JPAhibernate/spring jpa 中是否有任何设置来应对连接失败和与 Hibernate 和 Spring JPA 的重新连接
【发布时间】:2015-04-21 10:14:02
【问题描述】:

我有一个使用 Spring JPA 和 hibernate 连接到 mysql 数据库的 Web 应用程序。如果与数据库的连接由于某种原因断开,我想捕获该异常,以便我可以暂停处理,然后永远轮询该连接,直到它恢复。

hibernate/spring jpa 中是否有任何设置可以解决这个问题?

应用程序上下文.xml:

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
        <value>jdbc:mysql:${db.loc}</value>
    </property>
    <property name="username">
        <value>${db.username}</value>
    </property>
    <property name="password">
        <value>${db.password}</value>
    </property>
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="packagesToScan" value="com.georgelung.template" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

TemplateRepository 类

@Repository
public interface TemplateRepository extends CrudRepository<TestEntity, Long> {

}

【问题讨论】:

  • 需要贴出相关代码。

标签: java hibernate spring-data-jpa


【解决方案1】:

当hibernate提供开箱即用的解决方案时,为什么要捕获与数据库的连接丢失并尝试手动重新连接

在您的休眠配置中添加以下代码应该可以解决您的问题

<prop key="hibernate.connection.autoReconnect">true</prop>
<prop key="hibernate.connection.autoReconnectForPools">true</prop> 

【讨论】:

  • 这个问题是它有固定的重试次数(默认为 3),正如@Jordi Castilla 指出的那样,它不提供异常处理。还是有用的
【解决方案2】:

您需要像这样创建一个getConnection 方法以确保在尝试连接到数据库之前拥有有效的连接:

private Connection getConnection() {
   Connection connection = null;
   try {
       // This should return a NEW connection!
       Connection connection = dataSource.getConnection(); 
   } catch (Exception e) {
       // here you got the no connection found exception!
       System.err.println("Connection failed: " + e);
   }
   return connection;
}

【讨论】:

  • 我认为 OP 是在设置之后而不是基本的异常捕获。
  • 我建议了这个答案,因为在问题中 OP 说:如果与数据库的连接由于任何原因而断开,我想捕获该异常,,而不是实际询问关于设置自动重新连接...你认为@Pureferret?
  • 然后他们继续说“hibernate/spring jpa 中有什么设置可以解决这个问题吗?
  • 我不认为我的更好,我们同时回答并分享我们的知识,我问你是因为你为解决方案辩护,我想知道它是如何与这个 xml 配置一起工作的必要时添加到我未来的应用中以改进它们...
  • 我认为这只是两个答案采用完全不同的方法的事实,显然你选择这个而不是 hibernate.connection.autoReconnect 设置是有原因的。在您的回答中详细说明这一点将非常有帮助。
猜你喜欢
  • 2015-06-16
  • 1970-01-01
  • 2017-05-10
  • 1970-01-01
  • 2019-07-20
  • 1970-01-01
  • 2015-02-09
  • 2018-06-09
  • 1970-01-01
相关资源
最近更新 更多