I am running a simple test which inserts 1000 items into a database. When I run the test without connection pooling, the test completes in about 900ms. When I add C3P0 connection pooling, the test takes around 1400ms. The timing doesn't start until after the SessionFactory has been created, so the difference isn't attributable to that. I tried executing one insert before the timer starts in case the first insert has some one-time overhead, but it had no effect.
The only difference between the two tests is the following properties are added to the hibernate.cfg.xml file:
Code:
<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
Am I doing something wrong? Shouldn't the connection pooling speed things up dramatically?
Hibernate version: 3.2 Name and version of the database you are using: SQL Server 2005 ExpressHibernate configuration:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://SERVER/Test;instance=SQLEXPRESS;namedPipe=true</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<mapping resource="com/nextpage/domain/dto/TestItem.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Mapping documents:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.nextpage.domain.dto.TestItem" table="TestItem">
<id name="id" column="id"><generator class="guid"/></id>
</class>
</hibernate-mapping>
Code:Code:
package com.nextpage.domain.dto;
public class TestItem
{
private String id;
public TestItem()
{
}
public String getId()
{
return id;
}
private void setId(String id)
{
this.id = id;
}
}
Code:
import com.nextpage.domain.dto.TestItem;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Main
{
private static void testInsert(SessionFactory sessionFactory)
{
Session session = sessionFactory.openSession();
try
{
Transaction tx = session.beginTransaction();
TestItem item = new TestItem();
String itemId = (String)session.save(item);
tx.commit();
}
finally
{
session.close();
}
}
public static void main(String[] args) throws Exception
{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
try
{
testInsert(sessionFactory);
long start = System.currentTimeMillis();
for (int i = 0; i < 1000; ++i)
{
testInsert(sessionFactory);
}
System.out.println(System.currentTimeMillis() - start + "ms elapsed");
}
finally
{
sessionFactory.close();
}
}
}