List of usage examples for org.hibernate SessionFactory close
void close() throws HibernateException;
From source file:openones.oopms.requirement.dao.DeveloperDao.java
License:Apache License
public final boolean insertDeveloper(Developer dev) { log.debug("InsertDeveloperId.START"); try {//from w ww . j av a 2 s .co m SessionFactory sessFact = HibernateUtil.getSessionFactory(); session = sessFact.openSession(); Transaction tx = session.beginTransaction(); // Get max id // Query query = sess.createQuery(SQL_MAX_DEV_ID); String sql = "SELECT DEVELOPER_SEQ.NEXTVAL as nextValue FROM dual"; Query query = session.createSQLQuery(sql).addScalar("nextValue", Hibernate.BIG_DECIMAL); BigDecimal nextId = (BigDecimal) query.list().get(0); // Update next id dev.setDeveloperId(nextId); // Uppercase Account dev.setAccount(dev.getAccount().toUpperCase()); // Set BeginDate as default if null if (dev.getBeginDate() == null) { dev.setBeginDate(new Date()); } session.save(dev); tx.commit(); sessFact.close(); return true; } catch (RuntimeException rEx) { log.error("Saving Developer...", rEx); return false; } finally { session.close(); } }
From source file:org.apache.ignite.cache.hibernate.GridHibernateL2CacheConfigurationSelfTest.java
License:Apache License
/** * @param expCache1 Expected size of cache with name 'cache1'. * @param expCache2 Expected size of cache with name 'cache2'. * @param expCache3 Expected size of cache with name 'cache3'. * @param expCacheE3 Expected size of cache with name {@link #ENTITY3_NAME}. * @param expCacheE4 Expected size of cache with name {@link #ENTITY4_NAME}. *//*from ww w . j av a 2 s . c om*/ @SuppressWarnings("unchecked") private void testCacheUsage(int expCache1, int expCache2, int expCache3, int expCacheE3, int expCacheE4) { SessionFactory sesFactory = startHibernate(getTestGridName(0)); try { Session ses = sesFactory.openSession(); try { Transaction tx = ses.beginTransaction(); ses.save(new Entity1()); ses.save(new Entity2()); ses.save(new Entity3()); ses.save(new Entity4()); tx.commit(); } finally { ses.close(); } ses = sesFactory.openSession(); try { List<Entity1> list1 = ses.createCriteria(ENTITY1_NAME).list(); assertEquals(1, list1.size()); for (Entity1 e : list1) { ses.load(ENTITY1_NAME, e.getId()); assertNotNull(e.getId()); } List<Entity2> list2 = ses.createCriteria(ENTITY2_NAME).list(); assertEquals(1, list2.size()); for (Entity2 e : list2) assertNotNull(e.getId()); List<Entity3> list3 = ses.createCriteria(ENTITY3_NAME).list(); assertEquals(1, list3.size()); for (Entity3 e : list3) assertNotNull(e.getId()); List<Entity4> list4 = ses.createCriteria(ENTITY4_NAME).list(); assertEquals(1, list4.size()); for (Entity4 e : list4) assertNotNull(e.getId()); } finally { ses.close(); } IgniteCache<Object, Object> cache1 = grid(0).jcache("cache1"); IgniteCache<Object, Object> cache2 = grid(0).jcache("cache2"); IgniteCache<Object, Object> cache3 = grid(0).jcache("cache3"); IgniteCache<Object, Object> cacheE3 = grid(0).jcache(ENTITY3_NAME); IgniteCache<Object, Object> cacheE4 = grid(0).jcache(ENTITY4_NAME); assertEquals("Unexpected entries: " + toSet(cache1.iterator()), expCache1, cache1.size()); assertEquals("Unexpected entries: " + toSet(cache2.iterator()), expCache2, cache2.size()); assertEquals("Unexpected entries: " + toSet(cache3.iterator()), expCache3, cache3.size()); assertEquals("Unexpected entries: " + toSet(cacheE3.iterator()), expCacheE3, cacheE3.size()); assertEquals("Unexpected entries: " + toSet(cacheE4.iterator()), expCacheE4, cacheE4.size()); } finally { sesFactory.close(); } }
From source file:org.apache.ignite.cache.hibernate.HibernateL2CacheConfigurationSelfTest.java
License:Apache License
/** * @param expCache1 Expected size of cache with name 'cache1'. * @param expCache2 Expected size of cache with name 'cache2'. * @param expCache3 Expected size of cache with name 'cache3'. * @param expCacheE3 Expected size of cache with name {@link #ENTITY3_NAME}. * @param expCacheE4 Expected size of cache with name {@link #ENTITY4_NAME}. */// ww w .j av a 2s . co m @SuppressWarnings("unchecked") private void testCacheUsage(int expCache1, int expCache2, int expCache3, int expCacheE3, int expCacheE4) { SessionFactory sesFactory = startHibernate(getTestGridName(0)); try { Session ses = sesFactory.openSession(); try { Transaction tx = ses.beginTransaction(); ses.save(new Entity1()); ses.save(new Entity2()); ses.save(new Entity3()); ses.save(new Entity4()); tx.commit(); } finally { ses.close(); } ses = sesFactory.openSession(); try { List<Entity1> list1 = ses.createCriteria(ENTITY1_NAME).list(); assertEquals(1, list1.size()); for (Entity1 e : list1) { ses.load(ENTITY1_NAME, e.getId()); assertNotNull(e.getId()); } List<Entity2> list2 = ses.createCriteria(ENTITY2_NAME).list(); assertEquals(1, list2.size()); for (Entity2 e : list2) assertNotNull(e.getId()); List<Entity3> list3 = ses.createCriteria(ENTITY3_NAME).list(); assertEquals(1, list3.size()); for (Entity3 e : list3) assertNotNull(e.getId()); List<Entity4> list4 = ses.createCriteria(ENTITY4_NAME).list(); assertEquals(1, list4.size()); for (Entity4 e : list4) assertNotNull(e.getId()); } finally { ses.close(); } IgniteCache<Object, Object> cache1 = grid(0).cache("cache1"); IgniteCache<Object, Object> cache2 = grid(0).cache("cache2"); IgniteCache<Object, Object> cache3 = grid(0).cache("cache3"); IgniteCache<Object, Object> cacheE3 = grid(0).cache(ENTITY3_NAME); IgniteCache<Object, Object> cacheE4 = grid(0).cache(ENTITY4_NAME); assertEquals("Unexpected entries: " + toSet(cache1.iterator()), expCache1, cache1.size()); assertEquals("Unexpected entries: " + toSet(cache2.iterator()), expCache2, cache2.size()); assertEquals("Unexpected entries: " + toSet(cache3.iterator()), expCache3, cache3.size()); assertEquals("Unexpected entries: " + toSet(cacheE3.iterator()), expCacheE3, cacheE3.size()); assertEquals("Unexpected entries: " + toSet(cacheE4.iterator()), expCacheE4, cacheE4.size()); } finally { sesFactory.close(); } }
From source file:org.bpmscript.test.hibernate.HibernateSessionFactoryTestSupport.java
License:Apache License
public void execute(ITestCallback<IServiceLookup> callback) throws Exception { // final BasicDataSource dataSource = new BasicDataSource(); // dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver"); // dataSource.setUrl("jdbc:derby:test;create=true"); // dataSource.setUsername("sa"); // dataSource.setPassword("sa"); ///*from ww w . j a v a2s. co m*/ // Properties properties = new Properties(); // properties.setProperty("hibernate.hbm2ddl.auto", "update"); // properties.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyDialect"); // //properties.setProperty("hibernate.dialect", MyDerbyDialect.class.getName()); // properties.setProperty("hibernate.show_sql", "true"); // // final AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean(); // sessionFactoryBean.setLobHandler(new DefaultLobHandler()); // sessionFactoryBean.setHibernateProperties(properties); // sessionFactoryBean.setAnnotatedClasses(classes); // sessionFactoryBean.setDataSource(dataSource); // sessionFactoryBean.afterPropertiesSet(); // SessionFactory sessionFactory = (SessionFactory) sessionFactoryBean.getObject(); // cfg.setProperty("hibernate.dialect", DerbyDialect.class.getName()); // cfg.setProperty("hibernate.connection.driver_class", EmbeddedDriver.class.getName()); // cfg.setProperty("hibernate.connection.url", "jdbc:derby:lockingtest;create=true"); // cfg.setProperty("hibernate.connection.username", "sa"); // cfg.setProperty("hibernate.connection.password", "sa"); AnnotationConfiguration cfg = new AnnotationConfiguration().addAnnotatedClass(HibernateInstance.class); cfg.setProperty("hibernate.dialect", DerbyDialect.class.getName()); cfg.setProperty("hibernate.connection.driver_class", org.h2.Driver.class.getName()); cfg.setProperty("hibernate.connection.url", "jdbc:h2:~/bpmscript"); cfg.setProperty("hibernate.connection.username", "sa"); cfg.setProperty("hibernate.connection.password", ""); cfg.setProperty("hibernate.hbm2ddl.auto", "create"); cfg.setProperty("hibernate.show_sql", "true"); for (Class<?> clazz : classes) { cfg.addAnnotatedClass(clazz); } final SessionFactory sessionFactory = cfg.buildSessionFactory(); ServiceLookup serviceLookup = new ServiceLookup(); serviceLookup.addService("sessionFactory", sessionFactory); try { callback.execute(serviceLookup); } finally { sessionFactory.close(); //sessionFactoryBean.destroy(); } }
From source file:org.bpmscript.test.hibernate.SpringSessionFactoryTestSupport.java
License:Apache License
public void execute(ITestCallback<IServiceLookup> callback) throws Exception { // final BasicDataSource dataSource = new BasicDataSource(); // dataSource.setDriverClassName("com.mysql.jdbc.Driver"); // dataSource.setUrl("jdbc:mysql://localhost:3306/bpmscript"); // dataSource.setUsername("bpmscript"); // dataSource.setPassword("sa"); // //from ww w . j av a 2 s.c o m // Properties properties = new Properties(); // properties.setProperty("hibernate.hbm2ddl.auto", "create"); // properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); // properties.setProperty("hibernate.show_sql", "false"); final BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("org.h2.Driver"); dataSource.setUrl("jdbc:h2:~/bpmscript"); dataSource.setUsername("sa"); dataSource.setPassword(""); Properties properties = new Properties(); properties.setProperty("hibernate.hbm2ddl.auto", "create"); properties.setProperty("hibernate.dialect", H2Dialect.class.getName()); properties.setProperty("hibernate.show_sql", "false"); // final BasicDataSource dataSource = new BasicDataSource(); // dataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver"); // dataSource.setUrl("jdbc:derby:test;create=true"); // dataSource.setUsername("sa"); // dataSource.setPassword("sa"); // // Properties properties = new Properties(); // properties.setProperty("hibernate.hbm2ddl.auto", "update"); // properties.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyDialect"); // properties.setProperty("hibernate.query.substitutions", "true 1, false 0"); // properties.setProperty("hibernate.show_sql", "false"); ServiceLookup lookup = new ServiceLookup(); final AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean(); sessionFactoryBean.setLobHandler(new DefaultLobHandler()); sessionFactoryBean.setHibernateProperties(properties); sessionFactoryBean.setAnnotatedClasses(classes); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.afterPropertiesSet(); SessionFactory sessionFactory = (SessionFactory) sessionFactoryBean.getObject(); lookup.addService("sessionFactory", sessionFactory); try { callback.execute(lookup); } finally { sessionFactory.close(); sessionFactoryBean.destroy(); } }
From source file:org.geolatte.common.automapper.AutoMapperTest.java
License:Open Source License
@Test public void testStandard() throws Exception { doWithinConnection(// www . j a v a2 s. c om "create table testautomap (id integer primary key, name varchar, num int, price double, geometry geometry)"); doWithinConnection( "insert into testautomap values (1, 'test', 2, 2.43, ST_GeomFromText('POINT(1 1)', 4326))"); AutoMapConfiguration cfg = new AutoMapConfiguration(new TypeMapper("BLOB")); TableRef tblRef = TableRef.valueOf("TESTAUTOMAP"); cfg.addTable(tblRef); //Test that the configuration is successful final AutoMapper autoMapper = new AutoMapper(cfg, disposableCL()); final DatabaseMapping dbMapping = runAutoMapper(autoMapper); Document ormDoc = dbMapping.generateHibernateMappingDocument(); assertNotNull(ormDoc); LOGGER.debug("Mapping file:\n" + ormDoc.asXML()); assertEquals(cfg.getPackageName(), ormDoc.selectSingleNode("//hibernate-mapping/@package").getText()); assertEquals(cfg.getPackageName() + ".Testautomap", dbMapping.getTableMapping(TableRef.valueOf("TESTAUTOMAP")).getGeneratedClass().getCanonicalName()); assertEquals("Testautomap", ormDoc.selectSingleNode("//hibernate-mapping/class/@name").getText()); assertEquals("TESTAUTOMAP", ormDoc.selectSingleNode("//hibernate-mapping/class/@table").getText()); assertEquals("id", ormDoc.selectSingleNode("//hibernate-mapping/class/id/@name").getText()); assertEquals("integer", ormDoc.selectSingleNode("//hibernate-mapping/class/id/@type").getText()); assertEquals("GEOMETRY", ormDoc.selectSingleNode("//hibernate-mapping/class/property[@name='geometry']/@column").getText()); assertEquals("org.hibernatespatial.GeometryUserType", ormDoc.selectSingleNode("//hibernate-mapping/class/property[@name='geometry']/@type").getText()); assertEquals("geometry", dbMapping.getGeometryProperty(TableRef.valueOf("TESTAUTOMAP"))); assertEquals("id", dbMapping.getIdProperty(TableRef.valueOf("TESTAUTOMAP"))); //check if information can be retrieved final SessionFactory factory = buildSessionFactory(ormDoc); doWithinTransaction(factory, new TxOp() { public void execute(Session session) { Criteria criteria = session .createCriteria(dbMapping.getGeneratedClass(TableRef.valueOf("TESTAUTOMAP"))); List list = criteria.list(); assertTrue(list.size() == 1); } }); factory.close(); }
From source file:org.geolatte.common.automapper.AutoMapperTest.java
License:Open Source License
@Test public void testConfiguredId() throws Exception { doWithinConnection(//w ww.j a v a 2 s .c o m "create table testautomap (id integer, name varchar, num int, price double, geometry geometry)"); doWithinConnection( "insert into testautomap values (1, 'test', 2, 2.43, ST_GeomFromText('POINT(1 1)', 4326))"); doWithinConnection( "insert into testautomap values (1, 'test 2', 3, 2.43, ST_GeomFromText('POINT(2 2)', 4326))"); AutoMapConfiguration cfg = new AutoMapConfiguration(new TypeMapper("BLOB")); TableRef tblRef = TableRef.valueOf("TESTAUTOMAP"); cfg.addTableConfiguration(new TableConfiguration.Builder(tblRef).identifier("ID").result()); //Test that the configuration is successful final AutoMapper autoMapper = new AutoMapper(cfg, disposableCL()); final DatabaseMapping dbMapping = runAutoMapper(autoMapper); Document ormDocument = dbMapping.generateHibernateMappingDocument(); assertNotNull(ormDocument); LOGGER.debug("Mapping file:\n" + ormDocument.asXML()); assertEquals("Testautomap", ormDocument.selectSingleNode("//hibernate-mapping/class/@name").getText()); assertEquals("TESTAUTOMAP", ormDocument.selectSingleNode("//hibernate-mapping/class/@table").getText()); assertEquals("id", ormDocument.selectSingleNode("//hibernate-mapping/class/id/@name").getText()); assertEquals("id", dbMapping.getIdProperty(TableRef.valueOf("TESTAUTOMAP"))); //check if information can be retrieved final SessionFactory factory = buildSessionFactory(ormDocument); doWithinTransaction(factory, new TxOp() { public void execute(Session session) { Criteria criteria = session .createCriteria(dbMapping.getGeneratedClass(TableRef.valueOf("TESTAUTOMAP"))); List list = criteria.list(); assertTrue(list.size() == 2); } }); factory.close(); }
From source file:org.gridgain.grid.cache.hibernate.GridHibernateL2CacheConfigurationSelfTest.java
License:Open Source License
/** * @param expCache1 Expected size of cache with name 'cache1'. * @param expCache2 Expected size of cache with name 'cache2'. * @param expCache3 Expected size of cache with name 'cache3'. * @param expCacheE3 Expected size of cache with name {@link #ENTITY3_NAME}. * @param expCacheE4 Expected size of cache with name {@link #ENTITY4_NAME}. *//* ww w.ja va 2 s. c o m*/ @SuppressWarnings("unchecked") private void testCacheUsage(int expCache1, int expCache2, int expCache3, int expCacheE3, int expCacheE4) { SessionFactory sesFactory = startHibernate(getTestGridName(0)); try { Session ses = sesFactory.openSession(); try { Transaction tx = ses.beginTransaction(); ses.save(new Entity1()); ses.save(new Entity2()); ses.save(new Entity3()); ses.save(new Entity4()); tx.commit(); } finally { ses.close(); } ses = sesFactory.openSession(); try { List<Entity1> list1 = ses.createCriteria(ENTITY1_NAME).list(); assertEquals(1, list1.size()); for (Entity1 e : list1) { ses.load(ENTITY1_NAME, e.getId()); assertNotNull(e.getId()); } List<Entity2> list2 = ses.createCriteria(ENTITY2_NAME).list(); assertEquals(1, list2.size()); for (Entity2 e : list2) assertNotNull(e.getId()); List<Entity3> list3 = ses.createCriteria(ENTITY3_NAME).list(); assertEquals(1, list3.size()); for (Entity3 e : list3) assertNotNull(e.getId()); List<Entity4> list4 = ses.createCriteria(ENTITY4_NAME).list(); assertEquals(1, list4.size()); for (Entity4 e : list4) assertNotNull(e.getId()); } finally { ses.close(); } GridCache<Object, Object> cache1 = grid(0).cache("cache1"); GridCache<Object, Object> cache2 = grid(0).cache("cache2"); GridCache<Object, Object> cache3 = grid(0).cache("cache3"); GridCache<Object, Object> cacheE3 = grid(0).cache(ENTITY3_NAME); GridCache<Object, Object> cacheE4 = grid(0).cache(ENTITY4_NAME); assertEquals("Unexpected entries: " + cache1.entrySet(), expCache1, cache1.size()); assertEquals("Unexpected entries: " + cache2.entrySet(), expCache2, cache2.size()); assertEquals("Unexpected entries: " + cache3.entrySet(), expCache3, cache3.size()); assertEquals("Unexpected entries: " + cacheE3.entrySet(), expCacheE3, cacheE3.size()); assertEquals("Unexpected entries: " + cacheE4.entrySet(), expCacheE4, cacheE4.size()); } finally { sesFactory.close(); } }
From source file:org.grouter.common.hibernate.HibernateUtilContextAware.java
License:Apache License
/** * Closes the current SessionFactory and releases all resources. * <p/>//from w w w . jav a2s . c om * You need to call rebuildSessionFactory() to reinit the HibernateUtil's sessionfactories in the * internal map of sessionfactories. * * @param removeAllConfigFromInternalMap if true it will clear the internal map of sessionfactories and config */ public static void shutdown(boolean removeAllConfigFromInternalMap) { log.debug("Shutting down Hibernate."); Iterator iter = hibernateConfigMap.values().iterator(); while (iter.hasNext()) { HibernateConfigItem hibernateConfigItem = (HibernateConfigItem) iter.next(); log.debug("Closing sessionfactory : " + hibernateConfigItem.getId()); SessionFactory sf = hibernateConfigItem.getSessionFactory(); if (sf != null) { sf.close(); } sf = null; log.debug("Closed sessionfactory : " + hibernateConfigItem.getId()); } if (removeAllConfigFromInternalMap) { log.info("Removing all configurations from the internal hibernate config map"); hibernateConfigMap.clear(); } hibernateConfigItemDefault = null; currentState = STATE.SHUTDOWN; log.info("Entered state : " + currentState); }
From source file:org.halyph.sessiondemo.Demo.java
License:Open Source License
public static void main(String[] args) { // configures settings from hibernate.cfg.xml // Hibernate 3 style // SessionFactory sessionFactory = new Configuration().configure() // .buildSessionFactory(); // Hibernate 4 style Configuration configuration = new Configuration(); configuration.configure();/* w w w .j av a 2 s . c om*/ ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); // ------------------------------ // create a couple of events... Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(new Event("Our very first event!", new Date())); session.save(new Event("A follow up event", new Date())); session.getTransaction().commit(); session.close(); // now lets pull events from the database and list them session = sessionFactory.openSession(); session.beginTransaction(); List result = session.createQuery("from Event").list(); for (Event event : (List<Event>) result) { System.out.println("Event (" + event.getDate() + ") : " + event.getTitle()); } session.getTransaction().commit(); session.close(); if (sessionFactory != null) { sessionFactory.close(); } }