Liferay Hibernate /太多连接

时间:2014-09-14 17:19:15

标签: hibernate liferay

我现在正在进行一个项目,我们正在使用Liferay和Hibernate构建它,我们几乎已经完成了我们已经部署了测试版但是我们一直遇到有关数据库连接的问题。

我们总是遇到“连接太多”。

我们直接使用hibernate而不是使用服务构建器。

我的问题是,是不是可以直接使用Hibernate?我们真的应该使用服务构建器吗如果我们可以直接使用Hibernate,你能指出我如何避免遇到的问题吗?

请帮助我,已经解决了好几天。

此致 星

下面的P.S是我的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">

com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">

jdbc:mysql://localhost/lportal?zeroDateTimeBehavior=convertToNull&amp;autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.pool_size">100</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.use_query_cache">false</property> 

<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!-- Mapping files -->
......
</session-factory>
</hibernate-configuration>

2 个答案:

答案 0 :(得分:1)

我会检查代码以确保在使用后释放我的连接(例如&#34; connection.close()&#34;在&#34; finally&#34;块中)。您可以不断增加配置中允许的最大连接数,但如果存在内存泄漏 - 无论如何它都不会有帮助。这些事情通常通过重构代码本身来解决。

ServiceBuilder并不是Liferay的唯一选择,但它有很多优点,价值显而易见。关于关闭连接的上述想法在ServiceBuilder中正确处理,就像许多其他事情一样。

答案 1 :(得分:0)

此问题的临时解决方案可以增加mysql允许的最大连接数。您可以通过以下两种方式之一来实现此目的。

第一:

登录mysql服务器并输入以下命令。

mysql> SET GLOBAL max_connections = 200;

这将增加最大连接数。但是,如果重新启动服务器,此设置将会更改。

第二

编辑文件/etc/my.cnf并增加此文件中的max_connection。

[mysqld]
local-infile=0
datadir=/var/lib/mysql
user=mysql
symbolic-links=0

max_connections = 100

保存更改并键入以下内容以重新启动mysqld:

/etc/init.d/mysqld restart

但是这个问题通常出现是因为没有正确关闭且无法使用的开放连接。

尝试查看访问数据库的所有资源,并检查是否正确处理了所有连接。

希望这有帮助。

相关问题