简体   繁体   English

PgPool Java / JDBC应用程序的负载平衡

[英]PgPool Load balancing for Java/JDBC application

After setting up all the possible settings of PgPool on CentOS, when I tested it using my Java application, I found that it is not working. 在CentOS上设置了PgPool的所有可能的设置之后,当我使用Java应用程序对其进行测试时,我发现它无法正常工作。
After reading manual on internet (you can find here ), I found that it will not work for JDBC statements if they have been set to false (for auto commit). 在网上阅读了手册(可以在此处找到)之后,我发现如果将JDBC语句设置为false(对于自动提交),它将无法使用。
Since I am using Hibernate, I am quiet sure that it is using transaction to set the values. 因为我使用的是Hibernate,所以我很安静地确定它正在使用事务来设置值。
My question is, if this is true, which method of is useful to replicate my databases. 我的问题是,如果这是真的,哪种方法对复制我的数据库很有用。 I hear about parallel mode, but I am not sure whether it will work for Java application. 我听说过并行模式,但是我不确定它是否适用于Java应用程序。 Can anybody guide and provide me samples for it? 有人可以指导并提供样品吗?

If by parallel mode what you meant is the transaction isolation level, from this page you can see that PostgreSQL supports 4 level of isolations , and it is configurable from hibernate by setting the property: hibernate.connection.isolation to 1, 2, 4, or 8, from lower level to the highest. 如果通过并行模式表示的是事务隔离级别,则从此页面可以看到PostgreSQL支持4级隔离 ,并且可以通过将hibernate.connection.isolation属性设置为1、2、4从hibernate进行配置。或8,从较低级别到最高级别。

Read committed is the default isolation level in PostgreSQL, one level above dirty read. 提交的读是PostgreSQL中的默认隔离级别,比脏读高1级。

Serialization is the highest level and it is very expensive, because if 2 transactions are to be made on the same table, there will be a lock, if locking happens more than the time out that was set on the Database/Hibernate, then it will throw a time out exception. 序列化是最高级别,而且非常昂贵,因为如果要在同一个表上进行2个事务,则会有一个锁,如果锁发生的时间超过了数据库/休眠设置的超时时间,那么它将抛出超时异常。

Not sure if you have heard about them but following are frameworks that can be used with hibernate to improve performance: 不知道您是否听说过它们,但是以下是可与休眠一起使用以提高性能的框架:

  • C3P0 for a more advanced connection pooling C3P0提供更高级的连接池
  • Ehcache for boosting performance by enabling cache Ehcache通过启用缓存来提高性能

They are easy to configure and do not depends on OS. 它们易于配置,并且不依赖于操作系统。 I did not have any experience with PgPool so I can't give comments on performance comparison. 我没有使用PgPool的经验,因此无法对性能比较发表评论。

Following are the sample hibernate settings that you might want to try: 以下是您可能想尝试的示例休眠设置:

<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.connection.isolation">4</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider</prop>
<prop key="net.sf.ehcache.configurationResourceName">WEB-INF/ehcache.xml</prop>

I hope this can help you in optimizing your application in regards of database transactions. 我希望这可以帮助您优化数据库事务方面的应用程序。 There are a lot more that you can actually check eg table indexing, or using a profiler to find out which transactions cost the most. 您实际上可以检查更多内容,例如表索引编制或使用分析器找出哪些事务花费最大。

Modification transactions at the end of business methods work as you describe: a BEGIN/END block is created containing all modification queries that are either all committed or rolled back. 业务方法末尾的修改事务按您所描述的那样工作:创建一个BEGIN / END块,其中包含所有已提交或回退的所有修改查询。

This is done by setting autocommit to false, but this does not mean that all queries made by Hibernate are done in this mode. 这是通过将autocommit设置为false来完成的,但这并不意味着Hibernate进行的所有查询都在此模式下完成。 The same query depending on the required isolation mode might be executed either in auto-commit or non auto-commit mode. 根据所需的隔离模式,可以在自动提交或非自动提交模式下执行相同的查询。

For the usual case of a transaction in READ_COMMITED mode, queries like find by Id or named queries will run in it's own database transaction with auto-commit true ( and so without a BEGIN/END block). 对于以READ_COMMITED模式进行事务的通常情况,诸如ID的查找或命名查询之类的查询将在其自身的数据库事务中运行,并自动提交true(因此没有BEGIN / END块)。

Find by Ids and other read queries will only trigger a BEGIN block if they are run in at least REPEATABLE_READ isolation mode. 如果按ID查找和其他读取查询至少以REPEATABLE_READ隔离模式运行,则只会触发BEGIN块。

This mean that if you use the default REPEATABLE_READ isolation mode, the load balancing will work fine because most select queries will run with in auto-commit = true. 这意味着,如果您使用默认的REPEATABLE_READ隔离模式,则由于大多数选择查询将以auto-commit = true运行,因此负载平衡将正常运行。

You can confirm this by logging all SQL queries sent to the database using for example log4jdbc . 您可以通过记录所有发送到数据库的SQL查询(例如使用log4jdbc)来确认这一点 This will print all the SQL actually sent to the database. 这将打印所有实际发送到数据库的SQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM