#81 Wrong handling of cached prepared statements in batch mode

closed
nobody
None
5
2010-06-04
2010-05-13
AdamWozPL
No

Hai

We have noticed a strange behavior of Hibernate+c3p0.
For the following pieces of code we’ve been expecting that only one row will be inserted into CG_TAROPT_LOG table but, unfortunately, two records are inserted into our table.

package dummy;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.persistence.EntityManager;

import org.hibernate.Session;

import pl.capgemini.tme.bonobo.common.PersistenceUtil;

public class C3P0BugReport
{
public static void performTransaction(boolean throwAnException) throws SQLException
{
final EntityManager em = PersistenceUtil.createOracleEntityManager();
PreparedStatement prepStat = null;

try
{
em.getTransaction().begin();

final Session session = (Session) em.getDelegate();

prepStat =
session
.connection()
.prepareStatement("INSERT INTO CG_TAROPT_LOG(CO_ID, ENTDATE, CS_SEQNO, DESCRIPTION) VALUES (?,?,?,?)");

prepStat.setLong (1, -665);
prepStat.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
prepStat.setInt (3, 1);
prepStat.setString (4, "time: " + System.currentTimeMillis());

prepStat.addBatch();

if(throwAnException)
throw new NullPointerException("my exception");

prepStat.executeBatch();

em.getTransaction().commit();
}
catch(Exception e)
{
System.out.println("exception: " + e);
}
finally
{
if(prepStat != null)
prepStat.close();
PersistenceUtil.close(em);
}
}

public static void main(String[] args) throws SQLException
{
performTransaction(true );
performTransaction(false);
}
}

In hibernate.cfg.xml we have a following setting related to prepared statement cache:
<property name="hibernate.c3p0.max_statements">250</property>

In c3p0.properties config file we have a following setting related to prepared statement cache:
c3p0.maxStatementsPerConnection=100

We have found a workaround but we’ve got a feeling that this is a problem in c3p0 library.

Kind regards,
Adam Wozniak

Discussion

  • Steve Waldman

    Steve Waldman - 2010-05-20
    • status: open --> pending
     
  • Steve Waldman

    Steve Waldman - 2010-05-20

    Adam,

    I hate to pass the buck, but I don't think this is a c3p0 issue. (If I'm wrong, though, I'm delighted to fix it!)

    I've taken your test program and modified it, removing all reference to middleware, so that it operates only using JDBC and c3p0. Under a variety of Connection cache settings, a single row is inserted as expected.

    If you can, please run this in your environment and let me know if it still misbehaves. (You should be able just to modify the package name and put Connection properties -- c3p0.jdbcUrl, c3p0.driverClass, c3p0.user, c3p0.password -- directly into c3p0.properties to get this to run. Alternatively you can use setter methods -- setJdbcUrl(), setDriverClass(), setUser(), setPassword() -- on the ComboPooledDataSource.)

    smiles,
    Steve

     
  • SourceForge Robot

    • status: pending --> closed
     
  • SourceForge Robot

    This Tracker item was closed automatically by the system. It was
    previously set to a Pending status, and the original submitter
    did not respond within 14 days (the time period specified by
    the administrator of this Tracker).

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks