亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 Hibernate 釋放連接?

如何使用 Hibernate 釋放連接?

開滿天機 2023-03-09 15:00:40
在我的網絡應用程序中發出一些請求后,我得到了這個異常:java.sql.SQLTransientConnectionException: HikariPool-1 - 連接不可用,請求在 30002 毫秒后超時。明確地說,我沒有配置連接池(Hikari)。application.properties 中有我所有的屬性:spring.datasource.url=jdbc:postgresql://localhost/authHibernatespring.datasource.username=postgresspring.datasource.password=postgresspring.jpa.show-sql=truespring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialectspring.datasource.driver-class-name=org.postgresql.Driver Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented.spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false  for exceptionslogging.level.org.springframework.security=DEBUGlogging.level.org.hibernate.SQL=DEBUGlogging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACElogging.level.org.hibernate.type=TRACE我的目標是在我的 Spring-Boot 應用程序中使用通常的 Hiberante(所有這些 JpaRepo<> 和 CrudRepo 不是一個簡單的 Spring Data 方式)。為此,我從 EntityManager 獲得了會話,并像在通常的 Hibernate 中一樣使用它。據我了解,數據源負責連接池。但是這個東西是JDBC的東西。我應該如何用 Hibernate 方式而不是 JDBC 方式修復我的連接池?有一個我的 Dao 類:@Componentpublic class GrowBoxDaoImpl implements GrowBoxDao {@Autowiredprivate EntityManagerFactory entityManagerFactory;@Overridepublic List<GrowBox> findByUser(Long userId) {    Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();    String hqlQuery = "from GrowBox gb where gb.responsibleUser.id =: userId";    Query query = session.createQuery(hqlQuery);    query.setParameter("userId", userId);    List growBoxes = query.getResultList();    session.close();    return growBoxes;}@Overridepublic GrowBox findById(Long id) {    Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();    GrowBox growBox = session.get(GrowBox.class, id);    session.close();    return growBox;}}}我有幾個實體和每個實體的 Dao 類和服務。還有一些使用我的服務的控制器。如果有幫助,我的應用程序有 Git Repo: https: //github.com/DennisKingsman/HibernateWithSpringBootExample
查看完整描述

1 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

在典型的 Hibernate with Spring-boot 案例中,您可以只包括

@PersistenceUnit
private EntitiyManager entityManager;

spring-boot 負責EntityManager自動創建 s。它還會自動保留一個 EntityManagerFactory。無需顯式使用EntityManagerFactory. 這稱為容器管理的事務。會話自動關閉。

EntityManager 作為ThreadLocal(由 Spring-boot)管理,因此您可能無法訪問另一個線程中的同一個。

另一種方法是使用應用程序管理的事務。閱讀https://docs.oracle.com/cd/E19798-01/821-1841/bnbra/index.html


查看完整回答
反對 回復 2023-03-09
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號