Second Level Cache
Last updated on Jun 15, 2020
Second Level Cache utilizes a typical reserve for all the session question of a session industrial facility. It is valuable on the off chance that you have various session objects from a session factory. Session Factory holds the second level store information. It is worldwide for all the session questions and not empowered of course.
Different sellers have given the execution of Second Level Cache:
- EH Cache
- OS Cachen
- Swarm Cache
- JBoss Cache
- Read-only
- Nonstrict-read-write
- Read-write
- Transaction
| Implementation | read-only | nonstrict-read-write | read-write | transactional |
| EH Cache | Yes | Yes | Yes | No |
| OS Cache | Yes | Yes | Yes | No |
| Swarm Cache | Yes | Yes | No | No |
| JBoss Cache | No | No | No | Yes |
Example of Hibernate and struts2 integration
- index.jsp
- User.java
- RegisterDao.java
- user.hbm.xml
- hibernate.cfg.xml
- struts.xml
- welcome.jsp
- web.xml
Advantage of Spring framework with hibernate:
The Spring structure gives Hibernate Template class, so you don't have to take after such a variety of steps like make Configuration, BuildSessionFactory, Session, starting and conferring exchange.
Using the Problem without spring : //creating configuration Configuration cfg=new Configuration(); cfg.configure("hibernate.cfg.xml"); //creating seession factory object SessionFactory factory=cfg.buildSessionFactory(); //creating session object Session session=factory.openSession(); //creating transaction object Transaction t=session.beginTransaction(); Employee e1=new Employee(111,"arun",40000); session.persist(e1);//persisting the object t.commit();//transaction is commited session.close(); Answer by using Template class of Spring : Employee e1=new Employee(111,"arun",40000); hibernateTemplate.save(e1); Methods of HibernateTemplate class:| No. | Method |
| 1) | void persist(Object entity) |
| 2) | Serializable save(Object entity) |
| 3) | void saveOrUpdate(Object entity) |
| 4) | void update(Object entity) |
| 5) | void delete(Object entity) |
| 6) | Object get(Class entityClass, Serializable id) |
| 7) | Object load(Class entityClass, Serializable id) |
| 8) | List loadAll(Class entityClass) |
- create table in the database
- create applicationContext.xml file
- create Employee.java file
- create employee.hbm.xml file
- create EmployeeDao.java file
- create InsertTest.java file
In this record, we are giving every one of the informations of the database in the BasicDataSource protest. This question is utilized as a part of the LocalSessionFactoryBean class protest, containing some different informations, for example, mappingResources and hibernate Properties. The protest of LocalSessionFactoryBean class is utilized as a part of the Hibernate Template class.How about we see the code of applicationContext.xml document.
File: applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin: @localhost:1521:xe"></property> <property name="username" value="system"></property> <property name="password" value="oracle"></property> </bean> <bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mappingResources"> <list> <value>employee.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="mysessionFactory"></property> </bean> <bean id="d" class="com.javaspot.EmployeeDao"> <property name="template" ref="template"></property> </bean> </beans> 6) InsertTest.java package com.javaspot; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class InsertTest { public static void main(String[] args) { Resource r=new ClassPathResource("applicationContext.xml"); BeanFactory factory=new XmlBeanFactory(r); EmployeeDao dao=(EmployeeDao)factory.getBean("d"); Employee e=new Employee(); e.setId(114); e.setName("varun"); e.setSalary(50000); dao.saveEmployee(e); } } Enabling automatic table creation, showing sql queries <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> </props>
