List of usage examples for org.hibernate SessionFactory close
void close() throws HibernateException;
From source file:com.relations.OrderTest.java
public static void main(String[] args) { SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = sf.openSession();/* www.j a v a2s .com*/ try { PurchaseOrderHeader hd = new PurchaseOrderHeader(); hd.setPonumber("PO102030"); hd.setPodate(new Date()); hd.setOrderValue(new BigDecimal(1220)); PurchaseOrderDetail det = new PurchaseOrderDetail(); det.setLineNumber(1); det.setItem("120301"); det.setQuantity(new BigDecimal(10201)); det.setPurchaseOrderHeader(hd); hd.setPurchaseOrderDetail(det); PurchaseOrderHeader hd1 = new PurchaseOrderHeader(); hd1.setPonumber("PO102031"); hd1.setPodate(new Date()); hd1.setOrderValue(new BigDecimal(12201)); PurchaseOrderDetail det1 = new PurchaseOrderDetail(); det1.setLineNumber(2); det1.setItem("120302"); det1.setQuantity(new BigDecimal(10202)); det1.setPurchaseOrderHeader(hd1); hd1.setPurchaseOrderDetail(det1); PurchaseOrderHeader hd2 = new PurchaseOrderHeader(); hd2.setPonumber("PO102032"); hd2.setPodate(new Date()); hd2.setOrderValue(new BigDecimal(12202)); PurchaseOrderDetail det2 = new PurchaseOrderDetail(); det2.setLineNumber(3); det2.setItem("120303"); det2.setQuantity(new BigDecimal(10202)); det2.setPurchaseOrderHeader(hd2); hd2.setPurchaseOrderDetail(det2); PurchaseOrderHeader hd3 = new PurchaseOrderHeader(); hd3.setPonumber("PO102033"); hd3.setPodate(new Date()); hd3.setOrderValue(new BigDecimal(12203)); PurchaseOrderDetail det3 = new PurchaseOrderDetail(); det3.setLineNumber(4); det3.setItem("120304"); det3.setQuantity(new BigDecimal(10203)); det3.setPurchaseOrderHeader(hd3); hd3.setPurchaseOrderDetail(det3); session.beginTransaction(); session.save(hd); session.save(hd1); session.save(hd2); session.save(hd3); //session.save(det); session.getTransaction().commit(); session.close(); sf.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.relations.TestClient.java
public static void main(String[] args) { SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = sf.openSession();//from w w w. j av a2 s . c om try { Orders ord = new Orders(); ord.setOrderNumber("ORD15001"); ord.setOrderValue(1200); Orders ord2 = new Orders(); ord2.setOrderNumber("ORD15002"); ord2.setOrderValue(1500); OrderDetail det1 = new OrderDetail("Item1", 1, ord); OrderDetail det2 = new OrderDetail("Item2", 2, ord); OrderDetail det3 = new OrderDetail("Item3", 3, ord); OrderDetail det4 = new OrderDetail("Item4", 4, ord2); OrderDetail det5 = new OrderDetail("Item5", 5, ord2); Set<OrderDetail> orderDet = new HashSet<OrderDetail>(); orderDet.add(det1); orderDet.add(det2); orderDet.add(det3); ord.setOrderDetail(orderDet); Set<OrderDetail> orderDet1 = new HashSet<OrderDetail>(); orderDet1.add(det4); orderDet1.add(det5); ord.setOrderDetail(orderDet); ord2.setOrderDetail(orderDet1); session.save(ord); session.save(ord2); // // session.save(det1); // session.save(det2); // session.save(det3); session.close(); sf.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.seer.datacruncher.persistence.manager.DBConnectionChecker.java
License:Open Source License
public void useSession() { SessionFactory sessions = null; // return if host unreachable if (!isHostReachable(host, port)) { status = false;/*from ww w . ja va 2s.c o m*/ attempts--; //after three attempts the timeout is doubled if (attempts % 3 == 0) { attempts = 0; timeout = 2 * timeout; } return; } try { sessions = cfg.buildSessionFactory(); session = sessions.openSession(); // open a new Session session.doWork(new Work() { public void execute(java.sql.Connection connection) throws SQLException { //connection accepted without problems status = true; attempts++; if (attempts % 3 == 0 && timeout / 2 > 0) { //three attempts success : timeout is //divided by 2 and remains > 0 timeout /= 2; } } }); } catch (Throwable ex) { //connection attempt error : the status is set to false status = false; attempts--; //after three attempts the timeout is doubled if (attempts % 3 == 0) { attempts = 0; timeout = 2 * timeout; } } finally { try { if (session != null) session.close(); } catch (Exception e) { log.error("Session connection error on " + host + ": " + e.getMessage()); } try { if (sessions != null) sessions.close(); } catch (Exception e) { log.error("Closing session connection error on " + host + ": " + e.getMessage()); } } }
From source file:com.sprhib.model.teste.java
public static void main(String[] args) { SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); Usuario u1 = new Usuario("asd", "s", "asd", "dsad"); Area a1 = new Area("asa", "d", "f", "ff"); Area a2 = new Area("adsfdsfdsa", "d", "r", "gg"); Area a3 = new Area("a5465", "d", "r", "gg"); Area a4 = new Area("sfdsa", "d", "r", "gg"); Usuario u2 = new Usuario("sssss", "s", "asd", "dsad"); Temperatura t1 = new Temperatura(25, 4, 5); Temperatura t2 = new Temperatura(25, 4, 5); Tipo tt1 = new Tipo("utah"); Unidade un1 = new Unidade(65456); a1.setUsuario(u1);//from w ww . ja v a 2 s . co m a2.setUsuario(u1); a3.setUsuario(u1); a4.setUsuario(u2); t1.setArea(a1); t2.setArea(a2); tt1.setArea(a1); un1.setTipo(tt1); session.beginTransaction(); session.save(u1); session.save(u2); session.save(a1); session.save(a2); session.save(a3); session.save(a4); session.save(t1); session.save(t2); session.save(tt1); session.save(un1); session.getTransaction().commit(); session.close(); sessionFactory.close(); }
From source file:com.stackoverflow.jersey.hibernate.SessionFactoryFactory.java
@Override public void dispose(SessionFactory factory) { factory.close(); }
From source file:com.syntelinc.BOK.ATM.transactionpkg.HibernateTransaction.java
private void AccountTrans(Transaction tran) throws IllegalArgumentException { Configuration cfg = new Configuration().configure(); SessionFactory sf = cfg.buildSessionFactory(); Session s = sf.openSession();/*from w ww .jav a 2 s. c om*/ org.hibernate.Transaction t = s.beginTransaction(); SQLQuery q; if (sessionMap.get("accounttype").equals("checking")) { tran = new CheckingTransaction(); q = s.createSQLQuery("select balance from checkingtrans where acctid=?"); } else { tran = new SavingsTransaction(); q = s.createSQLQuery("select balance from savingtrans where acctid=?"); } tran.setAcctid(Integer.parseInt((String) sessionMap.get("accountid"))); q.setInteger(0, tran.getAcctid()); List ld = q.list(); BigDecimal bal; if (ld.isEmpty()) bal = new BigDecimal(0); else bal = (BigDecimal) ld.get(ld.size() - 1); tran.setTime(new Date()); tran.setLocation("1234 North Vegas Street"); DecimalFormat df = new DecimalFormat("#.##"); df.setRoundingMode(RoundingMode.CEILING); tran.setDebitamt(Double.parseDouble((String) sessionMap.get("depositamt"))); tran.setDebitamt(Double.parseDouble(df.format(tran.getDebitamt()))); tran.setCreditamt(Double.parseDouble((String) sessionMap.get("withdrawamt"))); tran.setCreditamt(Double.parseDouble(df.format(tran.getCreditamt()))); switch ((String) sessionMap.get("type")) { case "cash": tran.setType(1); tran.setBalance(bal.doubleValue() + tran.getDebitamt() - tran.getCreditamt()); tran.setBalance(Double.parseDouble(df.format(tran.getBalance()))); break; case "check": tran.setType(2); tran.setBalance(bal.doubleValue()); tran.setBalance(Double.parseDouble(df.format(tran.getBalance()))); break; case "default": throw new IllegalArgumentException(); } sessionMap.put("balance", tran.getBalance()); s.save(tran); t.commit(); s.close(); sf.close(); }
From source file:com.test.HibernateDerbyLockingTest.java
License:Apache License
public void testSpring() throws Exception { Properties properties = new Properties(); properties.setProperty("hibernate.hbm2ddl.auto", "create"); properties.setProperty("hibernate.dialect", "org.bpmscript.hibernate.DerbyDialect"); properties.setProperty("hibernate.show_sql", "true"); properties.setProperty("hibernate.connection.driver_class", EmbeddedDriver.class.getName()); properties.setProperty("hibernate.connection.url", "jdbc:derby:lockingtest;create=true"); properties.setProperty("hibernate.connection.username", "sa"); properties.setProperty("hibernate.connection.password", "sa"); properties.setProperty("hibernate.connection.pool_size", "10"); final AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean(); sessionFactoryBean.setLobHandler(new DefaultLobHandler()); sessionFactoryBean.setHibernateProperties(properties); sessionFactoryBean.setAnnotatedClasses(new Class[] { Person.class }); sessionFactoryBean.afterPropertiesSet(); SessionFactory sessionFactory = (SessionFactory) sessionFactoryBean.getObject(); try {/*w ww. j ava2 s. c o m*/ runTest(sessionFactory); } finally { sessionFactory.close(); } }
From source file:com.test.HibernateDerbyLockingTest.java
License:Apache License
public void testSpringWithDataSource() throws Exception { final BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(EmbeddedDriver.class.getName()); dataSource.setUrl("jdbc:derby:lockingtest;create=true"); dataSource.setUsername("sa"); dataSource.setPassword("sa"); Properties properties = new Properties(); properties.setProperty("hibernate.hbm2ddl.auto", "create"); properties.setProperty("hibernate.dialect", "org.bpmscript.hibernate.DerbyDialect"); properties.setProperty("hibernate.show_sql", "true"); // properties.setProperty("hibernate.connection.driver_class", // EmbeddedDriver.class.getName()); // properties.setProperty("hibernate.connection.url", // "jdbc:derby:lockingtest;create=true"); // properties.setProperty("hibernate.connection.username", "sa"); // properties.setProperty("hibernate.connection.password", "sa"); // properties.setProperty("hibernate.connection.pool_size", "10"); final AnnotationSessionFactoryBean sessionFactoryBean = new AnnotationSessionFactoryBean(); sessionFactoryBean.setLobHandler(new DefaultLobHandler()); sessionFactoryBean.setHibernateProperties(properties); sessionFactoryBean.setAnnotatedClasses(new Class[] { Person.class }); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.afterPropertiesSet(); SessionFactory sessionFactory = (SessionFactory) sessionFactoryBean.getObject(); try {// w ww. ja v a2 s.c o m runTest(sessionFactory); } finally { sessionFactory.close(); } }
From source file:com.test.HibernateDerbyLockingTest.java
License:Apache License
public void testHibernate() throws Exception { AnnotationConfiguration cfg = new AnnotationConfiguration().addAnnotatedClass(Person.class); 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"); cfg.setProperty("hibernate.connection.pool_size", "10"); cfg.setProperty("hibernate.hbm2ddl.auto", "create"); cfg.setProperty("hibernate.show_sql", "true"); final SessionFactory sessionFactory = cfg.buildSessionFactory(); try {/*w w w .j a v a2s. c o m*/ runTest(sessionFactory); } finally { sessionFactory.close(); } }
From source file:com.tremolosecurity.unison.k8s.dataobjects.CreateLocalUsers.java
License:Apache License
@Override public void configure(String name, Properties props, NameSpace nameSpace) throws LDAPException { StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(); Configuration config = new Configuration(); config.setProperty("hibernate.connection.driver_class", props.getProperty("driver")); config.setProperty("hibernate.connection.password", props.getProperty("password")); config.setProperty("hibernate.connection.url", props.getProperty("url")); config.setProperty("hibernate.connection.username", props.getProperty("user")); config.setProperty("hibernate.dialect", props.getProperty("dialect")); config.setProperty("hibernate.hbm2ddl.auto", "update"); config.setProperty("show_sql", "true"); config.setProperty("hibernate.current_session_context_class", "thread"); config.setProperty("hibernate.c3p0.max_size", Integer.toString(10)); config.setProperty("hibernate.c3p0.maxIdleTimeExcessConnections", Integer.toString(10)); JaxbCfgHibernateConfiguration jaxbCfg = new JaxbCfgHibernateConfiguration(); jaxbCfg.setSessionFactory(new JaxbCfgSessionFactory()); JaxbCfgMappingReferenceType mrt = new JaxbCfgMappingReferenceType(); mrt.setClazz(LocalUser.class.getName()); jaxbCfg.getSessionFactory().getMapping().add(mrt); mrt = new JaxbCfgMappingReferenceType(); mrt.setClazz(LocalGroup.class.getName()); jaxbCfg.getSessionFactory().getMapping().add(mrt); LoadedConfig lc = LoadedConfig.consume(jaxbCfg); StandardServiceRegistry registry = builder.configure(lc).applySettings(config.getProperties()).build(); SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); Session session = sessionFactory.openSession(); List<LocalGroup> groups = session.createCriteria(LocalGroup.class).list(); if (groups.size() == 0) { session.beginTransaction();/*w w w . j a v a 2 s . c o m*/ LocalGroup admins = new LocalGroup(); admins.setName("administrators"); admins.setDescription( "System administrators with approval access for new projects and new cluster admins"); session.save(admins); LocalGroup sys = new LocalGroup(); sys.setName("System"); sys.setDescription("System level groups not assigned to local users"); session.save(sys); LocalGroup users = new LocalGroup(); users.setName("users"); users.setDescription("All users are members"); session.save(users); LocalUser sysUser = new LocalUser(); sysUser.setSub("system"); sysUser.setMail(""); sysUser.setGroups(new ArrayList<LocalGroup>()); sysUser.getGroups().add(sys); session.save(sysUser); session.getTransaction().commit(); } sessionFactory.close(); }