简体   繁体   English

JPAContainer保持数据库的连接打开

[英]JPAContainer keeps database's connections open

i'm having this problem: with vaadin 7, i've configured my persistence.xml , here's the code 我遇到了这个问题:使用vaadin 7,我已经配置了我的persistence.xml,这是代码

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd“version =”2.0“>

<persistence-unit name="xxxx">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
    <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://xxxxx;databaseName=xxxx;"/>
    <property name="javax.persistence.jdbc.user" value="xxxx"/>
    <property name="javax.persistence.jdbc.password" value="xxxx"/>
    <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
    <property name="hibernate.connection.release_mode" value="after_transaction" />
    </properties>
</persistence-unit>

When i create a new JPAContainer and i bind it to a Table, i see on MSSQL Activity Monitor my new connection, but if my session is timed out or logged out, i still see the connection on SQL. 当我创建一个新的JPAContainer并将其绑定到表时,我在MSSQL活动监视器上看到我的新连接,但如果我的会话超时或注销,我仍然看到SQL上的连接。

If i relog in my app, and i refresh the table, a new connection is opened, and so on. 如果我重新登录我的应用程序,并刷新表格,则会打开一个新连接,依此类推。

My problem then is that i'm having a tons of opened connections on my sql server that are never closed. 我的问题是,我的SQL服务器上有大量已打开的连接,从未关闭。

I've tried to add the property 我试图添加属性

<property name="hibernate.connection.release_mode" value="after_transaction" />

but nothing changed. 但没有改变。 Can you help me please? 你能帮我吗? What i'm missing? 我错过了什么? Thanks so much 非常感谢

Unless you have really strong reasons not to, I'd really suggest to take a modern Java EE server and use its provided JPA, transaction management and connection pooling. 除非你有充分的理由不这样做,否则我真的建议采用现代Java EE服务器并使用其提供的JPA,事务管理和连接池。 Define the connection into the server and use JTA transactions (jta-data-source tag in your persistence.xml). 定义与服务器的连接并使用JTA事务(persistence.xml中的jta-data-source标记)。 Another option is to base your application on Spring. 另一种选择是将您的应用程序基于Spring。 Many things can go wrong if you go "low level" like you seem to be doing. 如果你像你似乎正在做的那样“低水平”,很多事情都可能出错。 If you are already familiar with Hibernate, I'd choose Wildfly as it has Hibernate as its JPA provider. 如果你已经熟悉Hibernate,我会选择Wildfly,因为它有Hibernate作为它的JPA提供者。

Instead of JPAContainer it is better to connect your UI via a facade (EJB) and if you need a lazy loaded Vaadin Container, use eg MTable from Viritin or Lazy Query Container. 而不是JPAContainer最好通过外观(EJB)连接您的UI,如果您需要延迟加载的Vaadin容器,请使用例如来自Viritin的MTable或Lazy Query Container。 This webinar and eg this example project might help your to get started. 网络研讨会和此示例项目可能有助于您开始使用。

If you really have no solution but to go with low level "application managed JPA session management", you should ensure your correctly close your EntityManager instance. 如果您真的没有解决方案,但要使用低级“应用程序管理的JPA会话管理”,则应确保正确关闭EntityManager实例。 I'd create a helper method to your UI class that returns the same EntityManager for each jpacontainer in you UI. 我将为您的UI类创建一个辅助方法,为您的UI中的每个jpacontainer返回相同的EntityManager。 Then add DetachListener to your UI and close the entity manager there. 然后将DetachListener添加到UI并关闭实体管理器。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM