第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用 Hibernate 釋放連接?

如何使用 Hibernate 釋放連接?

開滿天機(jī) 2023-03-09 15:00:40
在我的網(wǎng)絡(luò)應(yīng)用程序中發(fā)出一些請求后,我得到了這個(gè)異常:java.sql.SQLTransientConnectionException: HikariPool-1 - 連接不可用,請求在 30002 毫秒后超時(shí)。明確地說,我沒有配置連接池(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我的目標(biāo)是在我的 Spring-Boot 應(yīng)用程序中使用通常的 Hiberante(所有這些 JpaRepo<> 和 CrudRepo 不是一個(gè)簡單的 Spring Data 方式)。為此,我從 EntityManager 獲得了會(huì)話,并像在通常的 Hibernate 中一樣使用它。據(jù)我了解,數(shù)據(jù)源負(fù)責(zé)連接池。但是這個(gè)東西是JDBC的東西。我應(yīng)該如何用 Hibernate 方式而不是 JDBC 方式修復(fù)我的連接池?有一個(gè)我的 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;}}}我有幾個(gè)實(shí)體和每個(gè)實(shí)體的 Dao 類和服務(wù)。還有一些使用我的服務(wù)的控制器。如果有幫助,我的應(yīng)用程序有 Git Repo: https: //github.com/DennisKingsman/HibernateWithSpringBootExample
查看完整描述

1 回答

?
慕后森

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊

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

@PersistenceUnit
private EntitiyManager entityManager;

spring-boot 負(fù)責(zé)EntityManager自動(dòng)創(chuàng)建 s。它還會(huì)自動(dòng)保留一個(gè) EntityManagerFactory。無需顯式使用EntityManagerFactory. 這稱為容器管理的事務(wù)。會(huì)話自動(dòng)關(guān)閉。

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

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


查看完整回答
反對 回復(fù) 2023-03-09
  • 1 回答
  • 0 關(guān)注
  • 132 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)