I've got a problem with 2.1.3 query caching - it doesn't seem to work for me (everything was fine under 2.1.2). I've stripped some test code out of my WL environment and brought it down to a minimal set of code -
Code:
package test;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.cfg.Configuration;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
public class HibernateBug {
public static void main(String[] args) {
BasicConfigurator.configure();
Logger.getRootLogger().setLevel(Level.INFO);
final HibernateBug thisInstance = new HibernateBug();
try {
final SessionFactory sf = thisInstance.initializeHibernate().buildSessionFactory();
thisInstance.testRunQuery(sf);
thisInstance.testRunQuery(sf);
} catch (HibernateException e) {
e.printStackTrace();
}
}
final Configuration initializeHibernate() throws HibernateException {
final Properties props = new Properties();
props.put("hibernate.connection.driver_class", "weblogic.jdbc.sqlserver.SQLServerDriver");
props.put("hibernate.connection.url", "jdbc:bea:sqlserver://localhost:1433;DatabaseName=test");
props.put("hibernate.connection.username", "test");
props.put("hibernate.connection.password", "test");
props.put("hibernate.c3p0.minPoolSize", "5");
props.put("hibernate.c3p0.maxPoolSize", "20");
props.put("hibernate.c3p0.timeout", "1800");
props.put("hibernate.c3p0.max_statement", "50");
props.put("hibernate.dialect", "net.sf.hibernate.dialect.SybaseDialect");
props.put("hibernate.use_outer_join", "true");
props.put("hibernate.show_sql", "true");
props.put("hibernate.cache.use_query_cache", "true");
props.put("hibernate.transaction.factory_class", "net.sf.hibernate.transaction.JDBCTransactionFactory");
return new Configuration().setProperties(props).addClass(SystemUser.class);
}
final void testRunQuery(final SessionFactory sf) throws HibernateException {
final Session s = sf.openSession();
try {
System.out.println((SystemUser)(s.getNamedQuery("SystemUser.by.lastName").setParameter("lastName", "Riordan").
setCacheable(true).uniqueResult()));
} finally{
s.close();
}
}
}
mapping file -
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="test.SystemUser"
table="SystemUser"
mutable="true"
lazy="true"
>
<cache usage="nonstrict-read-write"/>
<id name="userId" type="integer" unsaved-value="0" access="field">
<generator class="identity" />
</id>
<version name="version" access="field" />
<property name="firstName" type="string" length="100" not-null="true" access="field" />
<property name="lastName" type="string" length="100" not-null="true" access="field" />
</class>
<!-- Pre-packaged queries -->
<query name="SystemUser.by.lastName"><![CDATA[
from test.SystemUser user
where user.lastName = :lastName
]]>
</query>
</hibernate-mapping>
Under 2.1.2 this is the output -
Code:
1454 [main] INFO net.sf.hibernate.cache.UpdateTimestampsCache - starting update timestamps cache at region: net.sf.hibernate.cache.UpdateTimestampsCache
1454 [main] WARN net.sf.ehcache.hibernate.Plugin - Could not find configuration for net.sf.hibernate.cache.UpdateTimestampsCache. Configuring using the defaultCache settings.
1454 [main] INFO net.sf.hibernate.cache.QueryCache - starting query cache at region: net.sf.hibernate.cache.QueryCache
1454 [main] WARN net.sf.ehcache.hibernate.Plugin - Could not find configuration for net.sf.hibernate.cache.QueryCache. Configuring using the defaultCache settings.
Hibernate: select systemuser0_.userId as userId, systemuser0_.version as version, systemuser0_.firstName as firstName, systemuser0_.lastName as lastName from SystemUser systemuser0_ where (systemuser0_.lastName=? )
SystemUser:[1,0,Nick,Riordan]
1688 [main] WARN net.sf.hibernate.util.JDBCExceptionReporter - SQL Warning: 0, SQLState:
1688 [main] WARN net.sf.hibernate.util.JDBCExceptionReporter - [BEA][SQLServer JDBC Driver][SQLServer]Changed database context to 'test'.
1688 [main] WARN net.sf.hibernate.util.JDBCExceptionReporter - SQL Warning: 0, SQLState:
1688 [main] WARN net.sf.hibernate.util.JDBCExceptionReporter - [BEA][SQLServer JDBC Driver][SQLServer]Changed language setting to us_english.
SystemUser:[1,0,Nick,Riordan]
Under 2.1.3 this is the output -
Code:
1656 [main] INFO net.sf.hibernate.cache.UpdateTimestampsCache - starting update timestamps cache at region: net.sf.hibernate.cache.UpdateTimestampsCache
1656 [main] WARN net.sf.ehcache.hibernate.Plugin - Could not find configuration for net.sf.hibernate.cache.UpdateTimestampsCache. Configuring using the defaultCache settings.
1656 [main] INFO net.sf.hibernate.cache.QueryCache - starting query cache at region: net.sf.hibernate.cache.QueryCache
1656 [main] WARN net.sf.ehcache.hibernate.Plugin - Could not find configuration for net.sf.hibernate.cache.QueryCache. Configuring using the defaultCache settings.
Hibernate: select systemuser0_.userId as userId, systemuser0_.version as version, systemuser0_.firstName as firstName, systemuser0_.lastName as lastName from SystemUser systemuser0_ where (systemuser0_.lastName=? )
SystemUser:[1,0,Nick,Riordan]
1844 [main] WARN net.sf.hibernate.util.JDBCExceptionReporter - SQL Warning: 0, SQLState:
1844 [main] WARN net.sf.hibernate.util.JDBCExceptionReporter - [BEA][SQLServer JDBC Driver][SQLServer]Changed database context to 'test'.
1844 [main] WARN net.sf.hibernate.util.JDBCExceptionReporter - SQL Warning: 0, SQLState:
1844 [main] WARN net.sf.hibernate.util.JDBCExceptionReporter - [BEA][SQLServer JDBC Driver][SQLServer]Changed language setting to us_english.
Hibernate: select systemuser0_.userId as userId, systemuser0_.version as version, systemuser0_.firstName as firstName, systemuser0_.lastName as lastName from SystemUser systemuser0_ where (systemuser0_.lastName=? )
SystemUser:[1,0,Nick,Riordan]
Note that the select gets run twice under 2.1.3. Is anyone else seeing this problem?
- Nick