我使用Criteria搜索来自数据库的数据。但经过多次要求,我遇到了问题:
2016-07-14 17:54:08 ERROR o.h.e.jdbc.spi.SqlExceptionHelper - FATAL: sorry, too many clients already
这是我的功能出现错误:
public Page<T> findPageByCriteria(Map<String, Object> criteriaMap, List<String> order, List<String> dir) {
Integer start = (Integer) criteriaMap.get("start");
Integer limit = (Integer) criteriaMap.get("limit");
Criteria criteria = getPageCriteria(criteriaMap, order, dir);
// Pagination
List<BaseEntity> elements = null;
Integer totalCount = null;
if (start != null && limit != null) {
totalCount = criteria.list().size();
criteria.setFirstResult(start);
criteria.setMaxResults(limit);
elements = criteria.list();
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.rowCount());
projectionList.add(Projections.groupProperty(order.get(0)));
criteria.setProjection(projectionList);
criteria.setFirstResult(0);
criteria.setMaxResults(Integer.MAX_VALUE);
}
else {
elements = criteria.list();
totalCount = elements.size();
}
当我调用此函数时,它将在DB中创建1个连接,但其他功能则不是。我不知道为什么?
你能给我一些建议或建议我一些库来避免出现这个错误吗?
我认为我们在配置文件中增加max_connections是个好主意。我们没有永远增加它。
谢谢;)
更新
这是Hibernate配置:
@Bean
public LocalSessionFactoryBean sessionFactory() throws Exception {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setHibernateProperties(hibernateProperties());
sessionFactory.setMappingDirectoryLocations(
new ClassPathResource[] { new ClassPathResource("/.../../...") });
return sessionFactory;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put(DIALECT, environment.getRequiredProperty(DIALECT));
properties.put(SHOW_SQL, environment.getRequiredProperty(SHOW_SQL));
properties.put(FORMAT_SQL, environment.getRequiredProperty(FORMAT_SQL));
return properties;
}
@Bean
@Primary
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
@Bean
@Autowired
public HibernateTemplate hibernateTemplate(SessionFactory s) {
HibernateTemplate hibernateTemplate = new HibernateTemplate(s);
return hibernateTemplate;
}