List of usage examples for org.hibernate SessionFactory close
void close() throws HibernateException;
From source file:eu.optimis.ecoefficiencytool.trecdb.sp.EcoServiceTableDAO.java
License:Open Source License
public static Integer addEcoAssessment(String serviceId, double energyEffAssessment, double ecologicalEffAssessment) throws Exception { Session session = null;/*from w w w . j a va 2 s .c o m*/ Integer ret = null; SpEcoServiceTable serviceTable = new SpEcoServiceTable(); SessionFactory sf = HibernateUtil.getSessionFactory(); try { session = sf.openSession(); Transaction tx = session.beginTransaction(); tx.begin(); serviceTable.setServiceId(serviceId); serviceTable.setEnergyEffService(energyEffAssessment); serviceTable.setEcologicalEffService(ecologicalEffAssessment); ret = (Integer) session.save(serviceTable); tx.commit(); if (tx.wasCommitted()) { log.debug("Service " + serviceId + " assessment was commited correctly with id " + ret.toString() + "."); } else { log.error("Unable to insert service " + serviceId + " assessment data:"); } } catch (Exception e) { log.error("Unable to insert service " + serviceId + " assessment data: ", e); throw new Exception(e.toString()); } finally { if (session != null) { try { session.close(); } catch (HibernateException e) { } } } sf.close(); return ret; }
From source file:fi.uta.infim.usaproxylogdb.Main.java
License:Open Source License
/** * @param args//from w ww . j av a 2 s . co m * @throws Throwable */ @SuppressWarnings({ "deprecation" }) public static void main(String[] args) { printLicense(); UsaProxyLog loki; try { System.out.print("Parsing log file... "); loki = new UsaProxyLogParser().parseLog(args[0]); System.out.println("done."); } catch (ArrayIndexOutOfBoundsException ae) { printError(ae, "Please provide a file name.", true, true); return; } catch (Exception e) { printError(e, "Unable to parse log file.", true, true); return; } SessionFactory sf; try { System.out.print("Establishing database connection... "); Configuration cfg = new Configuration().configure(new File("hibernate.cfg.xml")) .addURL(UsaProxyLog.class.getResource("/META-INF/orm.xml")); sf = cfg.buildSessionFactory(); System.out.println("done."); } catch (Exception e) { printError(e, "Unable to configure database connectivity. Check your configuration.", true, true); return; } try { System.out.print("Persisting log contents... "); Session s = sf.openSession(); Transaction tx = s.beginTransaction(); s.persist(loki); tx.commit(); s.close(); sf.close(); System.out.println("done."); } catch (Exception e) { printError(e, "Unable to persist data.", true, true); return; } }
From source file:gitdemoprogect.GitDemoProgect.java
/** * @param args the command line arguments *//*from ww w .jav a 2s . co m*/ public static void main(String[] args) { // TODO code application logic here SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = sf.openSession(); try { Transaction tc = session.beginTransaction(); Student st = new Student(); st.setName("Naresh2"); st.setEmail("naresp2@inventrax.com"); st.setAddress("Visakhapatnam2"); st.setPhoneNumber("+917799006116"); String pk = (String) session.save(st); System.out.println("id=" + pk); tc.commit(); } catch (Exception ex) { ex.printStackTrace(); if (session != null) { session.close(); } if (sf != null) { sf.close(); } } finally { if (session != null) { session.close(); } if (sf != null) { sf.close(); } } }
From source file:gitdemoprogect.MappingsDemo.java
public static void main(String[] args) { try {/* w w w .j ava 2s.co m*/ Cart cart = new Cart(); cart.setName("MyCart"); Items item1 = new Items("I1", 10, 1, cart); Items item2 = new Items("I2", 20, 2, cart); Set<Items> itemsSet = new HashSet<Items>(); itemsSet.add(item1); itemsSet.add(item2); cart.setItems(itemsSet); cart.setTotal(10 * 1 + 20 * 2); SessionFactory sessionFactory = null; Session session = null; Transaction tx = null; //Get Session sessionFactory = HibernateUtil.getSessionFactory(); session = sessionFactory.openSession(); System.out.println("Session created"); //start transaction tx = session.beginTransaction(); //Save the Model objects session.save(cart); session.save(item1); session.save(item2); //Commit transaction tx.commit(); System.out.println("Cart ID=" + cart.getId()); session.close(); sessionFactory.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:gitdemoprogect.RelationsDemo.java
public static void main(String[] args) { SessionFactory sf = HibernateUtil.getSessionFactory(); Session session = sf.openSession();// ww w . j a va 2 s . c o m try { Employee emp = new Employee("Suresh", "suresh@inventrax.com", "+917799006116", "Visakhapatnam"); SoftwareEmployee se = new SoftwareEmployee(12, "Naresh", "naresh@inventrax.com", "+917799006116", "Visakhapatnam"); HardwareEmployee he = new HardwareEmployee("Hibernate", "Manikanta", "mani@inventrax.com", "+917799006226", "Visakhapatnam"); session.save(emp); session.save(se); session.save(he); session.beginTransaction().commit(); session.close(); // session.evict(emp); // session.evict(se); // session.evict(he); sf.close(); } catch (Exception ex) { ex.printStackTrace(); System.exit(0); } }
From source file:gov.nih.nci.security.upt.util.ClassPathLoader.java
License:BSD License
public static void releaseJarsFromClassPath(HttpSession session) { SessionFactory sf = (SessionFactory) session.getAttribute(DisplayConstants.HIBERNATE_SESSIONFACTORY); if (sf != null) { sf.close(); sf = null;/*from w w w.java 2 s. c o m*/ } Vector<String> v = new Vector<String>(); URLClassLoader sysloader = (URLClassLoader) Thread.currentThread().getContextClassLoader(); ClassLoaderUtil.releaseLoader(sysloader, v); File fileArray[] = (File[]) session.getAttribute(DisplayConstants.HIBERNATE_CONFIG_FILE_JAR); if (fileArray != null) { for (int i = 0; i < fileArray.length; i++) { if (fileArray[i] != null && fileArray[i].exists()) { fileArray[i].delete(); fileArray[i] = null; } } } }
From source file:gov.nih.nci.security.upt.util.JDBCHelper.java
License:BSD License
/** * This method uses Hibernates SessionFactory to get a Session and using Hibernates Criteria does a sample query * to connection and obtain results as part of testing for successful connection. * * Based on the kind of exceptions this method throws CSException with appropriate message. * @param appForm -//from w w w. j a v a2 s.com * The ApplicationForm with application database parameters to * test connection for. * @return String - The message indicating that connection and a SQL query * was successful * @throws CSException - * The exception message indicates which kind of application * database parameters are invalid. */ public static String testConnectionHibernate(Configuration configuration) throws CSException { SessionFactory sf = null; ResultSet rs = null; Statement stmt = null; Connection conn = null; Session session = null; try { //System.out.println("testConnectionHibernate*****1"); sf = configuration.buildSessionFactory(); //System.out.println("testConnectionHibernate*****2"); session = sf.openSession(); //System.out.println("testConnectionHibernate*****3"); conn = session.connection(); //System.out.println("testConnectionHibernate*****4"); stmt = conn.createStatement(); //System.out.println("testConnectionHibernate*****5"); stmt.execute("select count(*) from csm_application"); //System.out.println("testConnectionHibernate*****6"); rs = stmt.getResultSet(); //System.out.println("testConnectionHibernate*****7"); //System.out.println(rs.getMetaData().getColumnCount()); return DisplayConstants.APPLICATION_DATABASE_CONNECTION_SUCCESSFUL; } catch (Throwable t) { t.printStackTrace(); // Depending on the cause of the exception obtain message and throw a CSException. if (t instanceof SQLGrammarException) { throw new CSException( DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED + " " + t.getCause().getMessage()); } if (t instanceof JDBCConnectionException) { if (t.getCause() instanceof CommunicationsException) { throw new CSException(DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED_URL_SERVER_PORT); } if (t.getCause() instanceof SQLException) { throw new CSException(DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED_URL); } throw new CSException( DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED + " " + t.getMessage()); } if (t instanceof GenericJDBCException) { throw new CSException(DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED_URL_USER_PASS + " "); } if (t instanceof CacheException) { throw new CacheException("Please Try Again.\n "); } if (t instanceof HibernateException) { throw new CSException( DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED + " " + t.getMessage()); } throw new CSException(DisplayConstants.APPLICATION_DATABASE_CONNECTION_FAILED_URL_USER_PASS); } finally { try { sf.close(); rs.close(); stmt.close(); conn.close(); session.close(); } catch (Exception e) { } } }
From source file:government.service.CitizenService.java
@Override public String addCitizen(String name, String age, String provinceName) { ProvinceManager PM = new ProvinceManager(); CitizenManager CM = new CitizenManager(); SessionFactory SF = new Configuration().configure().buildSessionFactory(); Session session = SF.openSession();/*from w w w . ja v a 2 s . c om*/ Province p = PM.getProvinceByName(provinceName, session); // if province does not exist in database if (p == null) { PM.addProvince(new Province(provinceName), session); p = PM.getProvinceByName(provinceName, session); } Citizen newCitizen; newCitizen = new Citizen(p, name, Integer.parseInt(age)); CM.addCitizen(newCitizen, session); session.close(); SF.close(); return "Entry has been saved Successfully !"; }
From source file:government.service.CitizenService.java
@Override public String updateCitizen(int id, String newName, String newAge, String provinceName) { ProvinceManager PM = new ProvinceManager(); CitizenManager CM = new CitizenManager(); SessionFactory SF = new Configuration().configure().buildSessionFactory(); Session session = SF.openSession();/* ww w .ja v a 2 s.co m*/ Citizen citizen = CM.getCitizen(id, session); if (newAge != null) { if (!(newAge.isEmpty())) { if (Integer.parseInt(newAge) > 0) { citizen.setAge(Integer.parseInt(newAge)); } } } if (newName != null && !newName.isEmpty()) { citizen.setName(newName); } if (provinceName != null && !provinceName.isEmpty()) { Province p = PM.getProvinceByName(provinceName, session); citizen.setProvince(p); } CM.updateCitizen(id, citizen, session); session.close(); SF.close(); return "Entry has been update Successfully !"; }
From source file:government.service.CitizenService.java
@Override public String deleteCitizen(int id) { ProvinceManager PM = new ProvinceManager(); CitizenManager CM = new CitizenManager(); SessionFactory SF = new Configuration().configure().buildSessionFactory(); Session session = SF.openSession();/* w w w.j a v a 2s . c o m*/ CM.deleteCitizen(id, session); session.close(); SF.close(); return "Citizen has been terminated Successfully !"; }