I have an IdentifierGenerator in use in my application. It is responsible for generating ids for a few legacy objects that we have upgraded to Hibernate. When testing this by creating objects and checking the IDs, when not using conneciton pooling (DriverManger) all ids generated are unique. However, when I configure in hibernate.cfg.xml to use the c3p0 connection pool, all of a sudden every id that is generated is the same. There are no exceptions and nothing else has changed, just the addition of the connection pool. If I comment out the configuration and re run the tests they are all unique.
Any clue what could be happening?
Hibernate version: 3.0.5
Database: SQL Server 2000
Database Driver: jTDS 1.2
Mapping documents:
<hibernate-mapping>
<sql-query name="sp_getNextID" callable="true">
<return-scalar column="id_value" type="int"/>
{call sp_getNextID(:sequence)}
</sql-query>
</hibernate-mapping>
....
<id name="id" column="contact_id" length="13" unsaved-value="null">
<generator class="raider.util.LegacyIdGenerator">
<param name="sequence">contact_id</param>
</generator>
</id>
....
Hibernate config:
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://server</property>
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<property name="hibernate.connection.SendStringParametersAsUnicode">false</property>
<property name="hibernate.connection.DatabaseName">db_name</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.query.substitutions">true 1, false 0</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!--
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">100</property>
<property name="c3p0.min_size">0</property>
<property name="c3p0.timeout">100</property>
-->
Relevant Stored Procedure:
CREATE PROCEDURE sp_getNextID
@ID_NAME varchar(50)
AS
BEGIN TRANSACTION
SET NOCOUNT ON
UPDATE id_table
SET id_value = id_value + 1
WHERE id_name = @ID_NAME
SELECT id_value
FROM id_table
WHERE id_name = @ID_NAME
COMMIT TRANSACTION
GO
IdentifierGenerator:
Code:
public class LegacyIdGenerator implements IdentifierGenerator, Configurable
{
private static final String SEQUENCE_PARAM = "sequence";
private static final String QUERY_NAME = "sp_getNextID";
private String sequence;
public LegacyIdGenerator() {}
public LegacyIdGenerator(String sequence)
{
setSequence(sequence);
}
private void setSequence(String sequence)
{
this.sequence = sequence;
}
public void configure(Type type, Properties params, Dialect dialect)
{
setSequence(params.getProperty(SEQUENCE_PARAM));
}
public Serializable generate(SessionImplementor session, Object o)
{
return generate(new HibernateQuery(session.getNamedSQLQuery(QUERY_NAME)));
}
private String generate(HibernateQuery query)
{
query.setParameter(SEQUENCE_PARAM, sequence);
List results = query.execute();
if (results.isEmpty())
throw new IllegalArgumentException("No value associated with sequence '" + sequence + '\'');
return results.get(0).toString();
}
}
Thanks for any help,
Frank