我在PostgreSQL上使用带有hibernate的spring boot。
DB:PostgreSQL版本:9.6.8
SPRING-boot版本:
我的问题:
在两个请求GET调用之后,连接不可用 游泳池(或新游泳池)。
有人可以指出出了什么问题吗? 所以抛出异常
java.sql.SQLTransientConnectionException: SpringBootJPAHikariCP - Connection is not available, request timed out after 3000ms.
at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:667) ~[HikariCP-2.7.8.jar:na]
at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:183) ~[HikariCP-2.7.8.jar:na]
at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:148) ~[HikariCP-2.7.8.jar:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:128) ~[HikariCP-2.7.8.jar:na]
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:48) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
Caused by: java.sql.SQLTransientConnectionException: SpringBootJPAHikariCP - Connection is not available, request timed out after 3000ms.
at com.zaxxer.hikari.pool.HikariPool.createTimeoutException(HikariPool.java:667) ~[HikariCP-2.7.8.jar:na]
at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:183) ~[HikariCP-2.7.8.jar:na]
at com.zaxxer.hikari.pool.HikariPool.getConnection(HikariPool.java:148) ~[HikariCP-2.7.8.jar:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:128) ~[HikariCP-2.7.8.jar:na]
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
我已设置以下连接池属性
spring.datasource.hikari.minimumIdle=250
spring.datasource.hikari.maximumPoolSize=5
spring.datasource.hikari.idleTimeout=3000
spring.datasource.hikari.poolName=SpringBootJPAHikariCP
spring.datasource.hikari.maxLifetime=500
spring.datasource.hikari.connectionTimeout=3000
DAO课程:
@Component
public class NickNameDAO {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Transactional
public List<NickName> getUserDetails() {
SessionFactory session = AutoWiringUtils.getSessionFactory(entityManagerFactory);
Criteria criteria = session.openSession().createCriteria(NickName.class);
List<NickName> returnValues = criteria.list();
return returnValues;
}
}
控制器类:
public class MainController {
@GetMapping(path="/all")
public @ResponseBody Iterable<NickName> getAllUsers() {
// This returns a JSON or XML with the users
return nickNameService.getUserDetails();
}
}
服务类:
@Service
public class NickNameService {
@Autowired
private NickNameDAO nickNameDAO;
public List<NickName> getUserDetails() {
return nickNameDAO.getUserDetails();
}
}
会话创建者类
public class AutoWiringUtils {
public static SessionFactory getSessionFactory(EntityManagerFactory entityManagerFactory) {
if (entityManagerFactory.unwrap(SessionFactory.class) == null) {
throw new NullPointerException("factory is not a hibernate factory");
}
return entityManagerFactory.unwrap(SessionFactory.class);
}
}
更新:1
使用session.getCurrentSession
我面临异常
@Component
public class NickNameDAO {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Transactional
public List<NickName> getUserDetails() {
SessionFactory session = AutoWiringUtils.getSessionFactory(entityManagerFactory);
Criteria criteria = session.getCurrentSession().createCriteria(NickName.class);
List<NickName> returnValues = criteria.list();
return returnValues;
}
}
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3466) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1426) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1422) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
at org.springframework.orm.hibernate5.SessionFactoryUtils.flush(SessionFactoryUtils.java:147) ~[spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]