在我的網(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
添加回答
舉報(bào)
0/150
提交
取消