Search in sources :

Example 1 with Account

use of org.hibernate.test.cache.infinispan.functional.entities.Account in project hibernate-orm by hibernate.

the class SessionRefreshTest method testRefreshAfterExternalChange.

@Test
public void testRefreshAfterExternalChange() throws Exception {
    // First session factory uses a cache
    CacheContainer localManager = ClusterAwareRegionFactory.getCacheManager(DualNodeTest.LOCAL);
    localCache = localManager.getCache(Account.class.getName());
    SessionFactory localFactory = sessionFactory();
    // Second session factory doesn't; just needs a transaction manager
    SessionFactory remoteFactory = secondNodeEnvironment().getSessionFactory();
    AccountDAO dao0 = new AccountDAO(useJta, localFactory);
    AccountDAO dao1 = new AccountDAO(useJta, remoteFactory);
    Integer id = new Integer(1);
    dao0.createAccount(dao0.getSmith(), id, new Integer(5), DualNodeTest.LOCAL);
    // Basic sanity check
    Account acct1 = dao1.getAccount(id);
    assertNotNull(acct1);
    assertEquals(DualNodeTest.LOCAL, acct1.getBranch());
    // This dao's session factory isn't caching, so cache won't see this change
    dao1.updateAccountBranch(id, DualNodeTest.REMOTE);
    // dao1's session doesn't touch the cache,
    // so reading from dao0 should show a stale value from the cache
    // (we check to confirm the cache is used)
    Account acct0 = dao0.getAccount(id);
    assertNotNull(acct0);
    assertEquals(DualNodeTest.LOCAL, acct0.getBranch());
    log.debug("Contents when re-reading from local: " + TestingUtil.printCache(localCache));
    // Now call session.refresh and confirm we get the correct value
    acct0 = dao0.getAccountWithRefresh(id);
    assertNotNull(acct0);
    assertEquals(DualNodeTest.REMOTE, acct0.getBranch());
    log.debug("Contents after refreshing in remote: " + TestingUtil.printCache(localCache));
    // Double check with a brand new session, in case the other session
    // for some reason bypassed the 2nd level cache
    AccountDAO dao0A = new AccountDAO(useJta, localFactory);
    Account acct0A = dao0A.getAccount(id);
    assertNotNull(acct0A);
    assertEquals(DualNodeTest.REMOTE, acct0A.getBranch());
    log.debug("Contents after creating a new session: " + TestingUtil.printCache(localCache));
}
Also used : SessionFactory(org.hibernate.SessionFactory) Account(org.hibernate.test.cache.infinispan.functional.entities.Account) CacheContainer(org.infinispan.manager.CacheContainer) Test(org.junit.Test)

Example 2 with Account

use of org.hibernate.test.cache.infinispan.functional.entities.Account in project hibernate-orm by hibernate.

the class AccountDAO method getTotalBalance.

public int getTotalBalance(AccountHolder holder, boolean useRegion) throws Exception {
    List results = (List) withTxSessionApply(useJta, sessionFactory, session -> {
        Query query = session.createQuery("select account.balance from Account as account where account.accountHolder = ?");
        query.setParameter(0, holder);
        if (useRegion) {
            query.setCacheRegion("AccountRegion");
        }
        query.setCacheable(true);
        return query.list();
    });
    int total = 0;
    if (results != null) {
        for (Iterator it = results.iterator(); it.hasNext(); ) {
            total += ((Integer) it.next()).intValue();
            System.out.println("Total = " + total);
        }
    }
    return total;
}
Also used : AccountHolder(org.hibernate.test.cache.infinispan.functional.entities.AccountHolder) List(java.util.List) Iterator(java.util.Iterator) TxUtil.withTxSession(org.hibernate.test.cache.infinispan.util.TxUtil.withTxSession) SessionFactory(org.hibernate.SessionFactory) Query(org.hibernate.Query) Account(org.hibernate.test.cache.infinispan.functional.entities.Account) InfinispanMessageLogger(org.hibernate.cache.infinispan.util.InfinispanMessageLogger) TxUtil.withTxSessionApply(org.hibernate.test.cache.infinispan.util.TxUtil.withTxSessionApply) Query(org.hibernate.Query) Iterator(java.util.Iterator) List(java.util.List)

Example 3 with Account

use of org.hibernate.test.cache.infinispan.functional.entities.Account in project hibernate-orm by hibernate.

the class AccountDAO method updateAccountBranch.

public void updateAccountBranch(Integer id, String branch) throws Exception {
    withTxSession(useJta, sessionFactory, session -> {
        log.debug("Updating account " + id + " to branch " + branch);
        Account account = session.get(Account.class, id);
        log.debug("Set branch " + branch);
        account.setBranch(branch);
        session.update(account);
        log.debug("Updated account " + id + " to branch " + branch);
    });
}
Also used : Account(org.hibernate.test.cache.infinispan.functional.entities.Account)

Example 4 with Account

use of org.hibernate.test.cache.infinispan.functional.entities.Account in project hibernate-orm by hibernate.

the class AccountDAO method createAccount.

public void createAccount(AccountHolder holder, Integer id, Integer openingBalance, String branch) throws Exception {
    withTxSession(useJta, sessionFactory, session -> {
        log.debug("Creating account " + id);
        Account account = new Account();
        account.setId(id);
        account.setAccountHolder(holder);
        account.setBalance(openingBalance);
        log.debug("Set branch " + branch);
        account.setBranch(branch);
        session.persist(account);
        log.debug("Created account " + id);
    });
}
Also used : Account(org.hibernate.test.cache.infinispan.functional.entities.Account)

Example 5 with Account

use of org.hibernate.test.cache.infinispan.functional.entities.Account in project hibernate-orm by hibernate.

the class AccountDAO method updateAccountBalance.

public void updateAccountBalance(Integer id, Integer newBalance) throws Exception {
    withTxSession(useJta, sessionFactory, session -> {
        log.debug("Updating account " + id + " to balance " + newBalance);
        Account account = session.get(Account.class, id);
        account.setBalance(newBalance);
        session.update(account);
        log.debug("Updated account " + id + " to balance " + newBalance);
    });
}
Also used : Account(org.hibernate.test.cache.infinispan.functional.entities.Account)

Aggregations

Account (org.hibernate.test.cache.infinispan.functional.entities.Account)7 SessionFactory (org.hibernate.SessionFactory)2 AccountHolder (org.hibernate.test.cache.infinispan.functional.entities.AccountHolder)2 Iterator (java.util.Iterator)1 List (java.util.List)1 Properties (java.util.Properties)1 Query (org.hibernate.Query)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)1 TimestampsRegionImpl (org.hibernate.cache.infinispan.timestamp.TimestampsRegionImpl)1 InfinispanMessageLogger (org.hibernate.cache.infinispan.util.InfinispanMessageLogger)1 TestInfinispanRegionFactory (org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory)1 TxUtil.withTxSession (org.hibernate.test.cache.infinispan.util.TxUtil.withTxSession)1 TxUtil.withTxSessionApply (org.hibernate.test.cache.infinispan.util.TxUtil.withTxSessionApply)1 CacheContainer (org.infinispan.manager.CacheContainer)1 Test (org.junit.Test)1