Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

EhCache Sample Code

Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to write sample code for Ehcache which loads values into the cache from the database automatically if it is not present in cache.

I have a model which calls the DAO. The DAO has the implementation of creating the cache based on the ehcache.xml configuration. I fail to see the connection between the DAO and the CacheEntryFactory which has a method createEntry() which is supposed to load the cache with the values. Is EhCache supposed to call creatEntry() internally ? If so, it is not calling and nothing gets printed on the console.

Iam attaching the codes here. Sorry if it is too long. Thanks for all the help.

public class CacheModel
public static void testCache() throws Exception

VistaCacheDAO sampledao = new VistaCacheDAO();
VistaCacheVO VO = sampledao.fetchContent(1);
String name = VO.getName();
int age = VO.getAge();
System.out.println ("Name :");
System.out.println (name);
System.out.println ("Age :");
System.out.println (age);

public class CacheFactory implements CacheEntryFactory

public CacheFactory()


public Object createEntry(Object key) throws Exception
CacheEntryKey Key = (CacheEntryKey) key;
int new_ID = Key.getID();
CacheVO VO = new CacheVO();
String query = null;
Connection con = DriverManager.getConnection(
"jdbc:mysql://xxxxxxxx:xxxx/xxxxxx", "root", "root");
Statement st = con.createStatement();
ResultSet rs = null;
query = "select Name, Age from testcache where ID="+ new_ID;
rs = st.executeQuery(query);


return VO;

public class CacheDAO

public CacheVO fetchContent(int ID) throws Exception
CacheVO VO = null;
CacheEntryKey key = new CacheEntryKey();
CacheManager samplemanager = CacheManager
Cache cache = samplemanager
Serializable value = cache.get(key);
VO = (VistaCacheVO) value;
return VO;

public class CacheEntryKey implements Serializable
private int ID;

public int getID()
return ID;

public void setID(int id)
ID = id;

public class CacheVO implements Serializable
private int Age;

private String Name;

public String getName()
return Name;

public void setName(String name)
Name = name;

public int getAge()
return Age;

public void setAge(int age)
Age = age;

I think she's lovely. It's this tiny ad that called her crazy:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic