问题描述
我为数据库使用hibernate和c3p0,并且我有一个使用ThreadPoolExecutor的java应用程序。
我正在做的是,我正在设计与hibernate相关的不同任务,用Hibernate使用Transactions和getCurrentSession存储数据。
所以我有新的ThreadPoolExecutor(CORE_POOL_SIZE,MAX_POOL_SIZE,KEEP_ALIVE_TIME,TimeUnit.MINUTES,taskQueue);
Runnable
{
@Override
public void run(){
Session session = HibernateDAO.getInstance()。getCurrentSession();
Transaction tx = null;
尝试
{tx = session.beginTransaction(); .....
}
和hibernate.cfg
线程
org.hibernate.connection.C3P0ConnectionProvider
1
<! - Database c3p0 settings - >
< property name =hibernate.c3p0.min_size> 3< / property>
< property name =hibernate.c3p0.max_size> 3< / property>
< property name =hibernate.c3p0.initialPoolSize> 3< / property>
< property name =hibernate.c3p0.max_statements> 100< / property>
< property name =hibernate.c3p0.timeout> 5000< / property>
< property name =hibernate.c3p0.acquire_increment> 2< / property>
我的目标是从执行程序获取每个线程,使用getCurrentSession获得连接,并执行会话.saveOrUpdate(item),
现在我有几个问题。
我应该使用哪种隔离级别,因为我有95%的写入/更新和5%的读取,并且读取不会被写入中断。
我从log4j获得HTML日志
905 pool-1-thread-1 DEBUG com.mchange.v2 .resourcepool.BasicResourcePool trace com.mchange.v2.resourcepool.BasicResourcePool@434cb775 [managed:3,unused:0,excluded:0](例如com.mchange.v2.c3p0.impl.NewPooledConnection@697506e6)
905 pool-1-thread-5 DEBUG com.mchange.v2.resourcepool.BasicResourcePool trace com.mchange.v2.resourcepool.BasicResourcePool@434cb775 [managed:3,unused:0,excluded:0](例如com.mchange.v2。 c3p0.impl.NewPooledConnection@697506e6)
965 pool-1-thread-4 DEBUG com.mchange.v2.resourcepool.BasicResourcePool trace com.mchange.v2.resourcepool.BasicResourcePool@434cb775 [managed:3,unused:0 ,例外:0](例如com.mchange.v2.c3p0.impl.NewPooledConnection@697506e6)
这是否意味着我在c3p0中只有1个连接,或在一个池中有多个连接?
上午我使用它作为它应该使用????
$ b $如果任何人需要更多的数据和解释只是戳一下,那么,什么级别的隔离最适合并发写入? p
- 编辑:回答
正如答案中的建议,我检查了mysql服务器admin,有更多的连接用于quering ..所以它是正确的方式来使用它:D ...
至于隔离级别虐待尝试让/ b
这意味着您有一个包含3个初始连接的池。你应该能够看到你的SQL服务器监视器来证实这一点。
请记住,你的SQL服务器配置胜过C3PO,所以如果你有多个线程竞争同一个事务无论您的池配置如何,您都可以发现您的写入/更新操作被阻止。
您可能发现在单个事务中批量插入/更新可能会提供比将它们移动到单独的线程。 有趣的阅读。
至于指定隔离级别,我的理解是,这更像是 read 性能的问题。我从来没有发现自己处于一种低于 READ COMMITTED
的状态,因为允许使用 READ UNCOMMITTED $ c $首先完全消除了使用ACID数据库的优势。然而,我不是这个领域的专家,其他人可能会有不同的意见。如果这确实是您的担忧,我会根据您选择的数据库对两个设置进行基准测试,以确定可以获得多少性能(如果有的话)。
I am using hibernate and c3p0 for the database, and i have an java application, with ThreadPoolExecutor.What i am doing is, I am enquing different Tasks each related to hibernate, to store data with Hibernate using Transactions, and getCurrentSession.So I have
new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.MINUTES,taskQueue);
Runnable
{
@Override
public void run() {
Session session = HibernateDAO.getInstance().getCurrentSession();
Transaction tx = null;
try
{tx = session.beginTransaction(); .....
}
And hibernate.cfg thread org.hibernate.connection.C3P0ConnectionProvider 1
<!-- Database c3p0 settings -->
<property name="hibernate.c3p0.min_size">3</property>
<property name="hibernate.c3p0.max_size">3</property>
<property name="hibernate.c3p0.initialPoolSize">3</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
My goal is to get per thread from the executor, to get a connection using getCurrentSession, and execute session.saveOrUpdate(item),Now I have couple of questions.Which Isolation level should i use, because i have 95% writes/updates and 5% reads, and reads are not interupted by the writes.I get in the HTML log from log4j
905 pool-1-thread-1 DEBUG com.mchange.v2.resourcepool.BasicResourcePool trace com.mchange.v2.resourcepool.BasicResourcePool@434cb775 [managed: 3, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@697506e6)
905 pool-1-thread-5 DEBUG com.mchange.v2.resourcepool.BasicResourcePool trace com.mchange.v2.resourcepool.BasicResourcePool@434cb775 [managed: 3, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@697506e6)
965 pool-1-thread-4 DEBUG com.mchange.v2.resourcepool.BasicResourcePool trace com.mchange.v2.resourcepool.BasicResourcePool@434cb775 [managed: 3, unused: 0, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@697506e6)
Does this mean That i have only 1 Connection in c3p0, or multiple connections in one pool?
AM I USING IT AS IT SHOULD BE USED????
what level of isolation will be best for concurrent writes?
thanks in advance to all, if anyone needs more data and explanation just poke.
--EDIT: answered
As suggested from the answerd, I have checked the mysql server admin, and there are more connections which are used for quering.. so IT IS THE RIGHT WAY to use it :D...As for the isolation levels ill try to let
This means you have one pool with 3 initial connections. You should be able to confirm this looking at your SQL server monitor.
Remember that your SQL server config trumps C3PO's, so if you have multiple threads competing for transactions on the same table you could find your write/update operations blocked regardless of your pool configuration.
You might find batching your insert/updates within a single transaction might provide a better performance boost than moving them to separate threads. This article an interesting read.
As for specifying a level of isolation, my understanding is that this is more a question of read performance than for writes. I've never found myself in a situation where I would want anything less than READ COMMITTED
, since allowing dirty reads with READ UNCOMMITTED
completely obliterates the advantages of using an ACID database in the first place. However I'm not an expert in this field, someone else might have a different opinion. If this really is a concern for you, I would benchmark both settings against the database of your choice to determine how much performance can be gained, if any.
这篇关于hibernate c3p0 ThreadPoolExecutor连接池,我做对了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!