IGNITE-5531: Fixed hang in Hibernate example (caused by not closed SessionFactory).
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/59ea90dd Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/59ea90dd Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/59ea90dd Branch: refs/heads/ignite-2.1.2-exchange Commit: 59ea90dd1f231cf3722fa2b9d4df6e0891b7a40c Parents: 2cc0b73 Author: devozerov <[email protected]> Authored: Sun Jun 18 10:04:47 2017 +0300 Committer: devozerov <[email protected]> Committed: Sun Jun 18 10:04:47 2017 +0300 ---------------------------------------------------------------------- .../hibernate/HibernateL2CacheExample.java | 94 ++++++++++---------- 1 file changed, 47 insertions(+), 47 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/59ea90dd/examples/src/main/java-lgpl/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java-lgpl/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExample.java b/examples/src/main/java-lgpl/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExample.java index 28aa6ca..551e5d0 100644 --- a/examples/src/main/java-lgpl/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExample.java +++ b/examples/src/main/java-lgpl/org/apache/ignite/examples/datagrid/hibernate/HibernateL2CacheExample.java @@ -125,71 +125,71 @@ public class HibernateL2CacheExample { ) { URL hibernateCfg = ExamplesUtils.url(HIBERNATE_CFG); - SessionFactory sesFactory = createHibernateSessionFactory(hibernateCfg); + try (SessionFactory sesFactory = createHibernateSessionFactory(hibernateCfg)) { + System.out.println(); + System.out.println(">>> Creating objects."); - System.out.println(); - System.out.println(">>> Creating objects."); + final long userId; - final long userId; + Session ses = sesFactory.openSession(); - Session ses = sesFactory.openSession(); - - try { - Transaction tx = ses.beginTransaction(); + try { + Transaction tx = ses.beginTransaction(); - User user = new User("jedi", "Luke", "Skywalker"); + User user = new User("jedi", "Luke", "Skywalker"); - user.getPosts().add(new Post(user, "Let the Force be with you.")); + user.getPosts().add(new Post(user, "Let the Force be with you.")); - ses.save(user); + ses.save(user); - tx.commit(); + tx.commit(); - // Create a user object, store it in DB, and save the database-generated - // object ID. You may try adding more objects in a similar way. - userId = user.getId(); - } - finally { - ses.close(); - } + // Create a user object, store it in DB, and save the database-generated + // object ID. You may try adding more objects in a similar way. + userId = user.getId(); + } + finally { + ses.close(); + } - // Output L2 cache and Ignite cache stats. You may notice that - // at this point the object is not yet stored in L2 cache, because - // the read was not yet performed. - printStats(sesFactory); + // Output L2 cache and Ignite cache stats. You may notice that + // at this point the object is not yet stored in L2 cache, because + // the read was not yet performed. + printStats(sesFactory); - System.out.println(); - System.out.println(">>> Querying object by ID."); + System.out.println(); + System.out.println(">>> Querying object by ID."); - // Query user by ID several times. First time we get an L2 cache - // miss, and the data is queried from DB, but it is then stored - // in cache and successive queries hit the cache and return - // immediately, no SQL query is made. - for (int i = 0; i < 3; i++) { - ses = sesFactory.openSession(); + // Query user by ID several times. First time we get an L2 cache + // miss, and the data is queried from DB, but it is then stored + // in cache and successive queries hit the cache and return + // immediately, no SQL query is made. + for (int i = 0; i < 3; i++) { + ses = sesFactory.openSession(); - try { - Transaction tx = ses.beginTransaction(); + try { + Transaction tx = ses.beginTransaction(); - User user = (User)ses.get(User.class, userId); + User user = (User)ses.get(User.class, userId); - System.out.println("User: " + user); + System.out.println("User: " + user); - for (Post post : user.getPosts()) - System.out.println("\tPost: " + post); + for (Post post : user.getPosts()) + System.out.println("\tPost: " + post); - tx.commit(); + tx.commit(); + } + finally { + ses.close(); + } } - finally { - ses.close(); - } - } - // Output the stats. We should see 1 miss and 2 hits for - // User and Collection object (stored separately in L2 cache). - // The Post is loaded with the collection, so it won't imply - // a miss. - printStats(sesFactory); + // Output the stats. We should see 1 miss and 2 hits for + // User and Collection object (stored separately in L2 cache). + // The Post is loaded with the collection, so it won't imply + // a miss. + printStats(sesFactory); + } } finally { // Distributed cache could be removed from cluster only by #destroyCache() call.
