CacheLoader
usydrajani Mar 19, 2012 9:36 AMHi I need help in loading data from the database to infinispan with cacheloader. I have configured infinispan-config.xml file like this. I have created the tables in postgres.
<namedCache name="entity"> <loaders passivation="false" shared="false" preload="true"> <loader class="org.infinispan.loaders.jdbc.stringbased.JdbcStringBasedCacheStore" fetchPersistentState="true" purgeOnStartup="false"> <properties> <property name="stringsTableNamePrefix" value="bookentity" /> <property name="idColumnName" value="isbn" /> <property name="dataColumnName" value="authorname" /> <property name="databaseType" value="POSTGRES" /> <property name="datasourceJndiLocation" value="java:/PostgresDS" /> <!-- <property name="connectionFactoryClass" value="org.infinispan.loaders.jdbc.connectionfactory.PooledConnectionFactory" /> --> <property name="connectionFactoryClass" value="org.infinispan.loaders.jdbc.connectionfactory.ManagedConnectionFactory" /> <!-- <property name="connectionUrl" value="jdbc:postgresql://129.78.10.201:5432/test1" /> --> <property name="userName" value="postgres" /> <!--<property name="password" value="password" />--> <property name="driverClass" value="org.postgresql.Driver" /> <property name="idColumnType" value="INTEGER" /> <property name="dataColumnType" value="VARCHAR(255)" /> <property name="dropTableOnExit" value="false" /> <property name="createTableOnStart" value="true" /> </properties> </loader> </loaders>
My persistence.xml file is
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="InfiniBookSahredCache-ejbPU" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/PostgresDS</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.session_factory_name" value="SessionFactories/infinispan"/> <property name="javax.persistence.sharedCache.mode" value="ALL"/> <property name="hibernate.cache.use_second_level_cache" value="true" /> <property name="hibernate.cache.use_query_cache" value="true"/> <property name="hibernate.cache.region_prefix" value="infinispan" /> <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.infinispan.InfinispanRegionFactory"/> <property name="hibernate.cache.infinispan.cachemanager" value="java:CacheManager/entity"/> </properties> </persistence-unit> </persistence>
My BookSessionBean which has all the business logic for creating, listing books. What I want to do is to load the data into cache without manually doing a put command to cache. seems there should be a straight forward way of loading the cache with the data from database (with a cache loader set). I am lost. Please let me know how to do that with the EJB and also with a simple application.
@Stateless @Remote(BookInterface.class) public class BookSessionBean implements BookInterface{ @PersistenceContext (unitName = "InfiniBookSahredCache-ejbPU") protected EntityManager em; @Resource(name = "CacheManager/entity", mappedName = "java:CacheManager/entity") protected EmbeddedCacheManager cm; public void addBook(Integer isbn, String a, String t){ BookEntity bIns = new BookEntity(); bIns.setAuthor(a); bIns.setISBN(isbn); bIns.setTitle(t); em.persist(bIns); } public void listbooksTest() { Cache<Integer, BookEntity> c = cm.getCache("entity"); System.out.println(c.getConfiguration().getCacheLoaders().isEmpty()+"this is from cacheloader"); Query q = em.createQuery("SELECT b FROM BookEntity b"); System.out.println(c.isEmpty()+"THHHHHHHHHHHHHHHHHHHHHHHHHIIIIIIIIIIIIIIIIIISSSSSSis from cache"); // output is true System.out.println(c.getConfiguration().getCacheLoaders().isEmpty()+"this is from cacheloader"); // output flase System.out.println(c.getConfiguration().isCacheLoaderPreload()+" is preloaded"); //output true CacheLoaderManager manager2 = c.getAdvancedCache().getComponentRegistry().getComponent(CacheLoaderManager.class); CacheStore cs3 = manager2.getCacheStore(); try { //System.out.println(cs3.containsKey(1)+ "cs3.containsKey(1)"); cs3.loadAll(); System.out.println(cs3.containsKey(1)+".......cs3.containsKey(1)......."); //cs3.load(1); //cs3. } catch (CacheLoaderException e1) { e1.printStackTrace(); } System.out.println(c.isEmpty()); System.out.println(c.getConfiguration().getCacheLoaders().contains(1)+ "contain a key 1 after "); //assert cs2.load(1). for (InternalCacheEntry e : c.getAdvancedCache().getDataContainer().entrySet()){ System.out.println(e.toString()); System.out.println(e.getKey()+ "printing from internalCachEntry"); } }