简体   繁体   English

首次登录时无法为事务打开休眠会话

[英]Could not open Hibernate Session for transaction on First Login

I have a full operating Spring MVC Application running with Spring Security but I get the following error whenever the server hasn't been active in a while and someone tries to login:我有一个使用 Spring Security 运行的完整运行的 Spring MVC 应用程序,但是每当服务器一段时间没有活动并且有人尝试登录时,我都会收到以下错误:

HTTP Status 500 - Request processing failed; HTTP 状态 500 - 请求处理失败; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction;嵌套异常是 org.springframework.transaction.CannotCreateTransactionException: 无法打开 Hibernate Session 进行事务; nested exception is org.hibernate.TransactionException: JDBC begin transaction failed嵌套异常是 org.hibernate.TransactionException: JDBC begin transaction failed

Sometimes after (approximately 5 seconds later) it starts to work normally.有时(大约 5 秒后)它开始正常工作。

I searched and found this link but I don't know how to configure connection testing in the connection pool configuration.我搜索并找到了这个链接,但我不知道如何在连接池配置中配置连接测试。

Any advice?有什么建议吗?

EDIT:编辑:

I found this link to set the configuration pool, so I tried to implement it in my dataSource :我找到了这个链接来设置配置池,所以我尝试在我的dataSource实现它:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />

        <property name="url" value="jdbc:mysql://192.168.254.45:3306/7jogos" />
        <property name="password" value="..." />
        <property name="username" value="..." />


<!--      Configuration pool -->
        <property name="validationQuery" value="SELECT 1" />
        <property name="validationInterval" value="34000" />
        <property name="testOnBorrow" value="true" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="55" />


    </bean>

But I end up getting an error on validationInterval saying:但我最终在validationInterval上得到一个错误说:

Multiple annotations found at this line: - No setter found for property 'validationInterval' in class 'org.apache.commons.dbcp.BasicDataSource'在这一行找到多个注释: - 在类“org.apache.commons.dbcp.BasicDataSource”中找不到属性“validationInterval”的setter

Without a stacktrace it is hard to tell for sure, but you are most likely dealing with expired DB connections.没有堆栈跟踪很难确定,但您很可能正在处理过期的数据库连接。 Hibernate fails to start a transaction for you because the first statement it tries to run ("START TRANSACTION - via JDBC) fails. Again, I assume that because the underlying DB connection got invalidated in the meantime . Hibernate 无法为您启动事务,因为它尝试运行的第一个语句(“START TRANSACTION - via JDBC)失败。我再次假设,因为在此期间底层数据库连接无效

This can happen due to various reason:发生这种情况的原因有多种:

  • DB settings (most likely)数据库设置(最有可能)
  • network/firewall网络/防火墙
  • local settings, etc.本地设置等

Regardless of the source though, you have the option to ensure that this is not happening.不管来源如何,您都可以选择确保不会发生这种情况。 If you are using a JDBC pool (like Tomcat JDBC pool ), you can configure the pool to test and reopen connections if necessary.如果您使用 JDBC 池(如Tomcat JDBC 池),您可以配置该池以测试并在必要时重新打开连接。 The testOnBorrow setting for example would instruct the pool to constantly test the db connection before actually offering it to the application for use.例如,testOnBorrow 设置将指示池在实际将其提供给应用程序使用之前不断测试数据库连接。 You can fine-tune these settings to eliminate performance-related concerns.您可以微调这些设置以消除与性能相关的问题。

ADDITIONAL INFO附加信息

You are trying to implement the pooling fine, but you got the variable name: validationInterval wrong.您正在尝试很好地实现池化,但是变量名称:validationInterval 错误。

You are getting that error because the BasicDataSource does not have a variable named like that, so Spring fails to set the property.您收到该错误是因为 BasicDataSource 没有这样命名的变量,因此 Spring 无法设置该属性。 Take a look at the javadoc of the BasicDataSource ( link ) to see what configuration variables are available for you.查看 BasicDataSource 的 javadoc( 链接),了解哪些配置变量可供您使用。 The validationInterval variable can be applied on tomcat pooling implementation (what I suggested above), the one you choose does not have this feature. validationInterval 变量可以应用于 tomcat 池化实现(我上面建议的),您选择的那个没有这个功能。

Replace代替

<property name="validationQuery" value="SELECT 1" />
<property name="validationInterval" value="34000" />
<property name="testOnBorrow" value="true" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="55" />

With this:有了这个:

<property name="initialSize" value="10" />
<property name="maxActive" value="5" />
<property name="maxWait" value="5000" />

The answer is on my edit that was to add the following in my beans that have the connection data:答案在我的编辑中,即在具有连接数据的 bean 中添加以下内容:

<!--      Configuration pool -->
        <property name="validationQuery" value="SELECT 1" />
        <property name="validationInterval" value="34000" />
        <property name="testOnBorrow" value="true" />
        <property name="removeAbandoned" value="true" />
        <property name="removeAbandonedTimeout" value="55" />

I don't know why but @Paweł Głowacz 's answer didn't work for me.我不知道为什么,但 @Paweł Głowacz 的回答对我不起作用。 Thanks for your help guys谢谢你们的帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 休眠会话无法打开进行交易 - Hibernate Session Could not open for transaction CannotCreateTransactionException:无法打开休眠会话进行事务 - CannotCreateTransactionException: Could not open Hibernate Session for transaction 无法为事务打开Hibernate Session,JavaConfig - Could not open Hibernate Session for transaction, JavaConfig 为什么会发生这种“无法打开Hibernate Session进行交易”的情况? - why this “Could not open Hibernate Session for transaction” occured? 无法为事务打开 Hibernate Session; 嵌套异常是无法打开连接 - Could not open Hibernate Session for transaction; nested exception is Could not open connection org.springframework.transaction.CannotCreateTransactionException:无法打开Hibernate Session进行事务处理 - org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction CannotCreateTransactionException:无法打开事务的Hibernate会话(Hibernate,MySQL,MVC) - CannotCreateTransactionException: Could not open Hibernate Session for transaction(Hibernate, MySQL, MVC) SpringMVC和Hibernate:CannotCreateTransactionException:无法打开Hibernate Session进行事务处理; - SpringMVC & Hibernate : CannotCreateTransactionException: Could not open Hibernate Session for transaction; 无法为事务打开Hibernate会话:从类加载器中看不到ConnectionWrapper - Could not open Hibernate Session for transaction: ConnectionWrapper is not visible from class loader 无法为事务 JAVA MAVEN SPRING 打开 Hibernate 会话 - Could not open Hibernate Session for transaction JAVA MAVEN SPRING
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM