简体   繁体   中英

hibernate c3p0 ThreadPoolExecutor Connection pooling, am I doing it right?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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