gudii9
asked on
hibernate query cache
Hi,
I am running hibernate query cache example as below
When I run the client jav afile getting below error.
SLF4J: Failed to load class "org.slf4j.impl.StaticLogg erBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate: select userdetail0_.userId as userId0_, userdetail0_.userName as userName0_ from UserDetails userdetail0_ where userdetail0_.userId=1
Exception in thread "main" org.hibernate.SessionExcep tion: Session is closed!
at org.hibernate.impl.Abstrac tSessionIm pl.errorIf Closed(Abs tractSessi onImpl.jav a:72)
at org.hibernate.impl.Session Impl.creat eQuery(Ses sionImpl.j ava:1768)
at org.gp.gpr.hibernate.Hiber nate2ndCac he.main(Hi bernate2nd Cache.java :33)
How can i fix this error
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
HibernateQueryCache.jpg
I am running hibernate query cache example as below
my hibernate config file looks as below
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">root</property>
<property name="hibersnate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- second level cache enable after download jars -->
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping class="org.gp.gpr.dto.UserDetails"/>
<mapping class="org.gp.gpr.dto.Vehicle"/>
<mapping class="org.gp.gpr.dto.TwoWheeler"/>
<mapping class="org.gp.gpr.dto.FourWheeler"/>
</session-factory>
</hibernate-configuration>
USerDetails looks as below
package org.gp.gpr.dto;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
//import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.NamedNativeQuery;
import org.hibernate.annotations.Type;
//import org.hibernate.annotations.Entity;
@Entity
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
@NamedQuery(name="UserDetails.byId", query="from UserDetails where userId=?")
//@NamedNativeQuery(name="UserDetails.byName", query="select * from User_Details where username=?", resultClass=UserDetails.class)
@org.hibernate.annotations.Entity(selectBeforeUpdate=true)
public class UserDetails {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName+"from get";
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Hibernate client java looks as below
package org.gp.gpr.hibernate;
import java.util.Date;
import java.util.List;
import org.gp.gpr.dto.UserDetails;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Hibernate2ndCache {
public static void main(String[] args){
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
Query query=session.createQuery("from UserDetails user where user.userId=1");
query.setCacheable(true);
List users=query.list();
session.getTransaction().commit();
session.close();
Session session2=sessionFactory.openSession();
session2.beginTransaction();
Query query2=session.createQuery("from UserDetails user where user.userId=1");
query2.setCacheable(true);
users=query2.list();
//UserDetails user2=(UserDetails) session.get(UserDetails.class, 1);
session.getTransaction().commit();
session.close();
}
}
When I run the client jav afile getting below error.
SLF4J: Failed to load class "org.slf4j.impl.StaticLogg
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate: select userdetail0_.userId as userId0_, userdetail0_.userName as userName0_ from UserDetails userdetail0_ where userdetail0_.userId=1
Exception in thread "main" org.hibernate.SessionExcep
at org.hibernate.impl.Abstrac
at org.hibernate.impl.Session
at org.gp.gpr.hibernate.Hiber
How can i fix this error
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
HibernateQueryCache.jpg
your close the session before which i bold and try to access the same session below .
your not using session2 at all
your not using session2 at all
ASKER
i am not clear. How can i fix it. please advise
session.getTransaction().commit();
session.close();
Session session2=sessionFactory.openSession();
session2.beginTransaction();
Query query2=session.createQuery("from UserDetails user where user.userId=1");
query2.setCacheable(true);
users=query2.list();
//UserDetails user2=(UserDetails) session.get(UserDetails.class, 1);
session.getTransaction().commit();
session.close();
should be change to
session.getTransaction().commit();
session.close();
Session session2=sessionFactory.openSession();
session2.beginTransaction();
Query query2=session2.createQuery("from UserDetails user where user.userId=1");
query2.setCacheable(true);
users=query2.list();
//UserDetails user2=(UserDetails) session2.get(UserDetails.class, 1);
session2.getTransaction().commit();
session2.close();
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
session.close();
Session session2=sessionFactory.op
session2.beginTransaction(
Query query2=session.createQuery
query2.setCacheable(true);
users=query2.list();
//UserDetails user2=(UserDetails) session.get(UserDetails.cl
session.getTransaction().c
session.close();
your close the session before which i bold and try to access the same session below .
your not using session2 at all .