program using hibernate does not terminate

前端 未结 13 840
孤街浪徒
孤街浪徒 2020-12-25 10:14

I created a program using Hibernate.

The program reaches the main function end, nevertheless the program is running.

I wonder if it happens when Sessio

13条回答
  •  离开以前
    2020-12-25 10:48

    My answer is for Hibernate 4.3+ version, and I use it in my way.

    A example of Spring Annotation configured with Hibernate:

    //Using AutoCloseable 1.7 feature here to close context and 
    //suppress warning Resource leak: 'context' is never closed
    //Creating AutoClosebale AbstractApplicationContext Object context
    try (AbstractApplicationContext context = new AnnotationConfigApplicationContext(SpringAppConfig.class)) {
    
        SnacksMenu snacks = context.getBean(SnacksMenu.class);
        System.out.println(snacks.toString());
    
        //Creating AutoClosebale SessionFactory Object factory
        try (SessionFactory factory = getStandardServiceEnabledSessionFactory()){
    
            //Creating AutoClosebale Session Object session
            try (Session session = factory.openSession()) {
    
                SessionCounter.analysisSession(session);
                System.out.println("1: Opened Sessions under factory : " + SessionCounter.getOpenSessionsCount());
    
                Transaction transaction = session.beginTransaction();
    
                session.persist(snacks);
                transaction.commit();
    
                System.out.println(session.isConnected());
            }//Session Object session resource auto closed
    
        }//SessionFactory Object factory resource auto closed
    
        System.out.println("2: Opened Sessions under factory : " + SessionCounter.getOpenSessionsCount());
    
    }//AbstractApplicationContext Object context resource auto closed
    

提交回复
热议问题