Example usage for org.hibernate SessionFactory isClosed

List of usage examples for org.hibernate SessionFactory isClosed

Introduction

In this page you can find the example usage for org.hibernate SessionFactory isClosed.

Prototype

boolean isClosed();

Source Link

Document

Is this factory already closed?

Usage

From source file:aa.webapp.servlet.hibernate.listener.HibernateSessionFactoryListener.java

@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
    SessionFactory sessionFactory = (SessionFactory) servletContextEvent.getServletContext()
            .getAttribute("SessionFactory");
    if (sessionFactory != null && !sessionFactory.isClosed()) {
        logger.info("Closing sessionFactory");
        sessionFactory.close();/*from   ww  w . j a  va2s. c o m*/
    }
    logger.info("Released Hibernate sessionFactory resource");
}

From source file:control.Demo_HibernateFull.java

/**
 * @param args the command line arguments
 *///from   w ww .  j  a  va  2 s  . co m
public static void main(String[] args) throws ParseException {
    SessionFactory sessionFactory = NewHibernateUtil.getSessionFactory();

    Session session = NewHibernateUtil.getSessionFactory().openSession();

    //open the session
    session.beginTransaction();

    //insert
    //Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2018-05-21");
    //Livraison livraison = new Livraison(new BigDecimal(106), date);
    Client client = new Client(new BigDecimal(10), "Luc Sansom", "(999)999-9999");

    Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse("2000-06-01");
    Commande commande = new Commande(new BigDecimal(1), client, date2);

    Article article = new Article(new BigDecimal(10), new BigDecimal(5), new BigDecimal(10));

    LignecommandeId lignecommandeId = new LignecommandeId(new BigDecimal(1), new BigDecimal(10));

    Lignecommande lignecommande = new Lignecommande(lignecommandeId, commande, article, new BigDecimal(32));

    Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2018-05-21");
    Livraison livraison = new Livraison(new BigDecimal(107), date);

    DetaillivraisonId detaillivraisonId = new DetaillivraisonId(new BigDecimal(107), new BigDecimal(1),
            new BigDecimal(10));
    DetaillivraisonId detaillivraisonId2 = new DetaillivraisonId(new BigDecimal(109), new BigDecimal(1),
            new BigDecimal(10));

    Detaillivraison detaillivraison = new Detaillivraison(detaillivraisonId, livraison, lignecommande,
            new BigDecimal(3));
    Detaillivraison detaillivraison2 = new Detaillivraison(detaillivraisonId2, livraison, lignecommande,
            new BigDecimal(10));

    Set livraisonset = new HashSet();

    livraisonset.add(detaillivraison);
    livraisonset.add(detaillivraison2);

    Date date3 = new SimpleDateFormat("yyyy-MM-dd").parse("2018-02-22");
    Livraison mylivraison = new Livraison(new BigDecimal(109), date3, livraisonset);

    Detaillivraison detaillivraison3 = new Detaillivraison(detaillivraisonId2, mylivraison, lignecommande,
            new BigDecimal(11));

    mylivraison.setDetaillivraisons(livraisonset);

    //session.save(livraison);
    //session.save(detaillivraison);
    //session.save(mylivraison);
    //session.update(mylivraison);
    //session.merge(mylivraison);
    session.save(detaillivraison3);
    //livraison.setDetaillivraisons(livraisonset);

    //commit
    session.getTransaction().commit();

    //close session 
    if (session.isConnected()) {
        session.close();
    }
    if (!sessionFactory.isClosed()) {
        sessionFactory.close();
    }
}

From source file:de.decidr.model.transactions.HibernateTransactionCoordinator.java

License:Open Source License

/**
 * Creates a new HibernateTransactionCoordinator
 * /*w  ww.  j  a va 2  s .  co m*/
 * @param sessionFactory
 *            a source for new sessions created by your hibernate
 *            configuration.
 * @throws IllegalArgumentException
 *             if the session factory is closed or <code>null</code>.
 */
private HibernateTransactionCoordinator(SessionFactory sessionFactory) {
    logger.log(Level.DEBUG, "Creating HibernateTransactionCoordinator");

    if (sessionFactory == null) {
        throw new IllegalArgumentException("Session factory must not be null.");
    }

    if (sessionFactory.isClosed()) {
        throw new IllegalArgumentException("Session factory is closed.");
    }

    this.sessionFactory = sessionFactory;
    this.notifiedReceivers = new ArrayList<TransactionalCommand>();
    this.currentTransaction = null;
    this.transactionDepth = 0;
    this.session = null;
}

From source file:de.decidr.model.webservice.helper.DBAccess.java

License:Apache License

/**
 * @param dwfmID//from   ww  w .ja v a 2s .com
 * @param invokeNodeId
 * @return gets the relevatn DWDL and the relevant WSDL file for the proxy-service from the Database
 */
public DbDataBean getDBData(long dwfmID, long invokeNodeId) {

    DbDataBean dataBean = new DbDataBean();

    SessionFactory sf = new Configuration().configure().buildSessionFactory();

    // actial session
    Session se = sf.getCurrentSession();

    try {
        // save transaction to avoid NullPointerException on error handling.
        Transaction tr = null;

        try {
            // initialize new transaction
            tr = se.beginTransaction();

            execDeployedWorkflowModelQuery(dwfmID, dataBean, se);
            execFileQuery(getFileIDFromDwdl(dataBean, invokeNodeId), dataBean, se);
            execServerLoadViewQuery(ServerTypeEnum.Ode.toString(), dataBean, se);

            se.getTransaction().commit();

        } catch (Throwable e) {
            // if no transaction has been initialized try rollback

            if (tr != null) {
                tr.rollback();
                e.printStackTrace();
            }
        }
    } finally {
        // try to close session if this does not work 
        // at least try to close the transaction.
        try {
            if (se.isOpen()) {
                se.close();
            }
            if (!sf.isClosed()) {
                sf.close();
            }
        } catch (Throwable e) {
            se.disconnect();
        }
    }

    return dataBean;
}

From source file:de.metanome.backend.results_db.HibernateUtilTest.java

License:Apache License

/**
 * Test method for {@link HibernateUtil#shutdown()} <p/> After shutting down the database the
 * session factory should be closed and a new one created on the next start.
 *//*from   ww w  .  j  av  a  2  s .com*/
@Test
public void testShutdown() {
    // Setup
    SessionFactory originalSessionFactory = HibernateUtil.getSessionFactory();
    assertFalse(originalSessionFactory.isClosed());

    // Execute functionality
    HibernateUtil.shutdown();
    SessionFactory newOpenSessionFactory = HibernateUtil.getSessionFactory();

    // Check result
    assertTrue(originalSessionFactory.isClosed());
    assertNotSame(newOpenSessionFactory, originalSessionFactory);
}

From source file:demo_hibernate3.AppCtr.java

/**
 * @param args the command line arguments
 *///from www .  j av  a 2s.com
public static void main(String[] args) throws ParseException {
    SessionFactory sessionFactory = NewHibernateUtil.getSessionFactory();

    Session session = NewHibernateUtil.getSessionFactory().openSession();

    //open the session
    session.beginTransaction();

    //set date
    Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2018-05-16");

    //insert
    Client myClient = new Client(new BigDecimal(7007), "James Bond", "514007007");
    Commande myCommande = new Commande(new BigDecimal(100100100), myClient, date);

    myCommande.setNocommande(new BigDecimal(700700700));

    //save the object
    session.save(myClient);
    session.save(myCommande);

    //update the object
    session.update(myCommande);

    //delete the object
    session.delete(myCommande);
    session.delete(myClient);

    //commit
    session.getTransaction().commit();

    //select
    Query query = session.createQuery("from Commande");

    List<Commande> resultat = query.list();

    for (Commande line : resultat) {
        System.out.println("NO commande: " + line.getNocommande() + " Date commande: " + line.getDatecommande()
                + " No client: " + line.getClient().getNomclient());
    }
    System.out.println("**************************************************************");
    //select 2
    Query query2 = session.createQuery("Select c.noclient, c.nomclient, count(noCommande)"
            + " from Client c, Commande m where c.noclient = m.client.noclient group by c.noclient, c.nomclient");

    List<Object[]> results = query2.list();

    for (Object[] line : results) {
        System.out.println("No client: " + line[0] + " Nom client: " + line[1] + " Nb commande " + line[2]);
    }

    if (session.isConnected()) {
        session.close();
    }
    if (!sessionFactory.isClosed()) {
        sessionFactory.close();
    }

}

From source file:edu.jhuapl.dorset.components.HibernateServiceTest.java

License:Open Source License

@Test
public void testCreationOfSessionFactory() {
    Properties props = getProperties();
    Config conf = ConfigFactory.parseProperties(props);

    hs = new HibernateService(conf);
    SessionFactory sf = hs.getSessionFactory();
    assertNotNull(sf);//w w  w.j  a v  a 2s  .co  m
    assertFalse(sf.isClosed());

    // traverse through the session factory to get at configuration values
    SessionFactoryOptions sfo = sf.getSessionFactoryOptions();
    StandardServiceRegistry ssr = sfo.getServiceRegistry();
    ConfigurationService cs = ssr.getService(ConfigurationService.class);
    assertEquals(props.getProperty("hibernate.connection.driver_class"),
            cs.getSetting("hibernate.connection.driver_class", StandardConverters.STRING));
    assertEquals(props.getProperty("hibernate.connection.url"),
            cs.getSetting("hibernate.connection.url", StandardConverters.STRING));
    assertEquals(props.getProperty("hibernate.dialect"),
            cs.getSetting("hibernate.dialect", StandardConverters.STRING));
    assertEquals(props.getProperty("hibernate.hbm2ddl.auto"),
            cs.getSetting("hibernate.hbm2ddl.auto", StandardConverters.STRING));

    // check mapping
    ClassMetadata cm = sf.getClassMetadata(TestObject.class);
    String[] names = cm.getPropertyNames();
    assertEquals(1, names.length);
    assertEquals("name", names[0]);
    assertEquals("string", cm.getPropertyType("name").getName());
}

From source file:edu.jhuapl.dorset.components.HibernateServiceTest.java

License:Open Source License

@Test
public void testShutdown() {
    Properties props = getProperties();
    Config conf = ConfigFactory.parseProperties(props);
    hs = new HibernateService(conf);
    SessionFactory sf = hs.getSessionFactory();

    hs.shutdown();//from   w w w. ja  v  a2s  . c om

    assertTrue(sf.isClosed());
}

From source file:net.krotscheck.jersey2.hibernate.factory.HibernateSessionFactoryFactory.java

License:Apache License

/**
 * Dispose of the hibernate session./*from w  w w .jav a  2  s .  c o  m*/
 *
 * @param sessionFactory The session to dispose.
 */
@Override
public void dispose(final SessionFactory sessionFactory) {
    if (sessionFactory != null && !sessionFactory.isClosed()) {
        logger.info("Disposing of hibernate session factory.");
        sessionFactory.close();
    }
}

From source file:net.krotscheck.jersey2.hibernate.factory.HibernateSessionFactoryFactoryTest.java

License:Apache License

/**
 * Test provide and dispose.//from  www.  j av a  2  s.  c om
 */
@Test
public void testProvideDispose() {
    Configuration config = locator.getService(Configuration.class);
    HibernateSessionFactoryFactory factoryFactory = new HibernateSessionFactoryFactory(config, locator);
    SessionFactory factory = locator.getService(SessionFactory.class);

    // Assert that the factory is open.
    Assert.assertFalse(factory.isClosed());

    // Make sure that we can create a session.
    Session session = factory.openSession();
    Assert.assertNotNull(session);
    Assert.assertTrue(session.isOpen());

    // Make sure we can dispose of the factory.
    factoryFactory.dispose(factory);
    Assert.assertTrue(factory.isClosed());
    Assert.assertFalse(session.isOpen());

    // Make sure doing it twice won't fail.
    factoryFactory.dispose(factory);

    // Make sure passing null doesn't fail
    factoryFactory.dispose(null);
}