Java Code Examples for org.hibernate.Session.close()

The following are Jave code examples for showing how to use close() of the org.hibernate.Session class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: sjk   File: AppDaoTest.java   View Source Code Vote up 6 votes
@Test
public void testupdateIncrementDownload() {
    short catalog = 1;
    Integer subCatalog = 3;
    String sort = null;
    String order = null;
    int currentPage = 1;
    int pageSize = 10;
    Boolean noVirus = null;
    Boolean noAds = null;
    Boolean official = null;
    List<App> list = appDao.list(catalog, subCatalog, sort, order, currentPage, pageSize, noAds, noVirus, official);
    assertNotNull(list);
    assertTrue(list.size() > 0);
    App a = list.get(0);
    logger.info("Id:{}", a.getId());
    Session session = sessions.openSession();
    int delta = 9;
    int rows = appDao.updateIncrementDownload(session, a.getId(), delta);
    session.flush();
    assertTrue(rows > 0);
    rows = appDao.updateIncrementDownload(session, a.getId(), -delta);
    session.flush();
    session.close();
}
 
Example 2
Project: sjk   File: AppHistory4IndexDaoImpl.java   View Source Code Vote up 6 votes
@Override
public List<AppHistory4Index> getApps(int currentPage, int pageSize) {
    String hql = "from AppHistory4Index appIdx where appIdx.indexStatus<>1";

    Session session = null;
    try {
        session = this.sessions.openSession();

        Query q = session.createQuery(hql);
        q.setFirstResult(HibernateHelper.firstResult(currentPage, pageSize));
        q.setMaxResults(pageSize);
        return HibernateHelper.list(q);
    } catch (Exception e) {
        logger.error("error:", e);
        return null;
    } finally {
        if (session != null && session.isOpen()) {
            session.clear();
            session.close();
        }
    }
}
 
Example 3
Project: bdf2   File: ValidatorMaintain.java   View Source Code Vote up 6 votes
@DataResolver
public void saveValidatorEvents(Collection<ValidatorEvent> events){
	Session session=this.getSessionFactory().openSession();
	try{
		for(ValidatorEvent v:events){
			EntityState state=EntityUtils.getState(v);
			if(state.equals(EntityState.NEW)){
				v.setId(UUID.randomUUID().toString());
				session.save(v);
			}
			if(state.equals(EntityState.DELETED)){
				session.delete(v);
			}
			if(state.equals(EntityState.MODIFIED)){
				session.update(v);
			}
		}			
	}finally{
		session.flush();
		session.close();
	}
}
 
Example 4
Project: bdf2   File: ValidatorMaintain.java   View Source Code Vote up 6 votes
@DataResolver
public void saveValidatorProperties(Collection<ValidatorProperty> properties){
	Session session=this.getSessionFactory().openSession();
	try{
		for(ValidatorProperty v:properties){
			EntityState state=EntityUtils.getState(v);
			if(state.equals(EntityState.NEW)){
				v.setId(UUID.randomUUID().toString());
				session.save(v);
			}
			if(state.equals(EntityState.DELETED)){
				session.delete(v);
			}
			if(state.equals(EntityState.MODIFIED)){
				session.update(v);
			}
		}			
	}finally{
		session.flush();
		session.close();
	}
}
 
Example 5
Project: bdf2   File: UserMaintain.java   View Source Code Vote up 6 votes
@Expose
public void insertUserPosition(String username, String ids) {
	Session session = this.getSessionFactory().openSession();
	try{
		session.createQuery(
				"delete " + UserPosition.class.getName()
				+ " u where u.username = :username")
				.setString("username", username).executeUpdate();
		
		if (StringUtils.isNotEmpty(ids)) {
			UserPosition userPosition;
			for (String id : ids.split(",")) {
				userPosition = new UserPosition();
				userPosition.setId(UUID.randomUUID().toString());
				userPosition.setPositionId(id);
				userPosition.setUsername(username);
				session.save(userPosition);
			}
		}
	}finally{
		session.flush();
		session.close();
	}
}
 
Example 6
Project: TrabalhoCrisParte2   File: DAOCliente.java   View Source Code Vote up 6 votes
public List listar() {
    Session session = DAOHibernateUtil.getSession();
    List listaCliente = null;
    try {
        session.beginTransaction();           
        listaCliente = session.createQuery("From Cliente").list();
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex);
    }
    finally{
        session.close();
        return listaCliente;
    }
}
 
Example 7
Project: sjk   File: AbstractBaseMarketService.java   View Source Code Vote up 6 votes
@Override
public void resetMarketForIncrement(Date lastReqDate) {
    Session session = sessions.openSession();
    try {
        Market market = getMarket(session);
        market.setIncrementLastReqCurrentPage(0);
        market.setIncrementTotalPages(0);
        market.setIncrementLastTime(lastReqDate);
        marketDao.update(session, market);
        session.flush();
        session.clear();
        logger.debug("Reset market for increment! {}", market.getMarketName());
    } finally {
        session.close();
    }
}
 
Example 8
Project: djpt   File: DaoImpl.java   View Source Code Vote up 6 votes
public void updateBySql(String sql, Object[] vals) {
	Session session = hibernateTemplate.getSessionFactory().openSession();
	
	org.hibernate.Transaction tx = session.beginTransaction();
	
	SQLQuery query = session.createSQLQuery(sql);
	if(vals != null) {
		for(int i=0;i<vals.length;i++) {
			if(vals[i] instanceof Integer) {
				query.setInteger(i, (Integer)vals[i]);
			} else {
				query.setString(i, vals[i].toString());
			}
		}
	}
	query.executeUpdate();
	
	try {
		tx.commit();
	} catch (Exception e) {
		e.printStackTrace();
		tx.rollback();
	}
	session.close(); 
}
 
Example 9
Project: SandBoxNGSB   File: QuoteDAOImpl.java   View Source Code Vote up 5 votes
@Override
public void save(QuotePE pe) {
   	Session session = getSession();
	Transaction tx = session.beginTransaction();
	session.persist(pe);
	tx.commit();
	session.close();
}
 
Example 10
Project: bdf2   File: HibernateDao.java   View Source Code Vote up 5 votes
public Collection<?> query(DetachedCriteria detachedCriteria,int pageIndex,int pageSize,String dataSourceName){
	Session session=this.getSessionFactory(dataSourceName).openSession();
	try{
		return detachedCriteria.getExecutableCriteria(session).setFirstResult((pageIndex-1)*pageSize).setMaxResults(pageSize).list();
	}finally{
		session.flush();
		session.close();
	}
}
 
Example 11
Project: bdf2   File: GroupMaintain.java   View Source Code Vote up 5 votes
@DataResolver
public void saveGroups(Collection<Group> groups) throws Exception{
	IUser user=ContextHolder.getLoginUser();
	if(user==null){
		throw new NoneLoginException("Please login first");
	}
	String companyId=user.getCompanyId();
	if(StringUtils.isNotEmpty(getFixedCompanyId())){
		companyId=getFixedCompanyId();
	}
	Session session=this.getSessionFactory().openSession();
	try{
		for(Group g:groups){
			EntityState state=EntityUtils.getState(g);
			if(state.equals(EntityState.NEW)){
				g.setId(UUID.randomUUID().toString());
				g.setCompanyId(companyId);
				g.setCreateDate(new Date());
				session.save(g);
			}else if(state.equals(EntityState.MODIFIED)){
				session.update(g);
			}else if(state.equals(EntityState.DELETED)){
				roleService.deleteRoleMemeber(g.getId(), MemberType.Group);
				groupService.deleteGroupMemeber(g.getId(), MemberType.Group);
				session.delete(g);
			}
		}
	}finally{
		session.flush();
		session.close();
	}
}
 
Example 12
Project: Building-Web-Apps-with-Spring-5-and-Angular   File: DoctorDAOImpl.java   View Source Code Vote up 5 votes
@Override
public Doctor save(Doctor doctor) {
	Session session = this.sessionFactory.openSession();
	session.save(doctor);
	session.close();
	return doctor;
}
 
Example 13
Project: sjk   File: AppDaoTest.java   View Source Code Vote up 5 votes
@Test
public void testgetAppByPackageName() {
    String[] packagename = null;
    packagename = new String[] { "ab.gobang", "ab.gobanga", "abacus.contentmenu", "Abacus.Menu", "abc.AsianMM" };
    for (String s : packagename) {
        appDao.getAppByPackageName(s);
    }

    Session session = sessions.openSession();
    appDao.getAppByPackageName(session, "com.g6677.android.bsg");
    session.close();
}
 
Example 14
Project: bdf2   File: JdbcTransactionManager.java   View Source Code Vote up 5 votes
private void clean(){
	Map<String,Session> sessionMap=ContextHolder.getHibernateSessionMap();
	if(sessionMap==null){
		return;
	}
	for(Session session:sessionMap.values()){
		if(session!=null && session.isOpen()){
			session.flush();
			session.close();
			session=null;
		}
	}
	sessionMap.clear();		
}
 
Example 15
Project: OSWf-OSWorkflow-fork   File: HibernateStore.java   View Source Code Vote up 5 votes
public ProcessInstance findProcessInstance(long entryId) throws WorkflowStoreException {

       Session session = null;
       Transaction transaction = null;
       ProcessInstance workflowEntry = null;
       
       try {
           session = sessionFactory.openSession();
           transaction = session.beginTransaction();

           workflowEntry = loadEntry(session, entryId);

           transaction.commit();

       } catch (HibernateException hibernateException) {
           throw new WorkflowStoreException(hibernateException);
       } finally {

           if (transaction != null && transaction.isActive())
                transaction.rollback();

           if (session != null)
               session.close();
       }
       
       return workflowEntry;
   }
 
Example 16
Project: SandBoxNGSB   File: QuoteDAOImpl.java   View Source Code Vote up 5 votes
@SuppressWarnings("unchecked")
@Override
public List<QuotePE> getList() {
   	Session session = getSession();
   	Criteria criteria = session.createCriteria(QuotePE.class);
	List<QuotePE> list = (List<QuotePE>)criteria.list();
	session.close();
	
	return list;
}
 
Example 17
Project: maven_EWorld_OpenShift   File: Fetch.java   View Source Code Vote up 5 votes
synchronized public static UserDetails getUserFromId(String userid)
{
         UserDetails ud = null;
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("from UserDetails ud where ud.usr_id=:uid");
                qry.setParameter("uid", userid);
                ud=(UserDetails)qry.getSingleResult();
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                          System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                 return (ud);
            }
            else
            {
                 return (new UserDetails());
            }
}
 
Example 18
Project: Restaurant   File: ProductDaoImpl.java   View Source Code Vote up 4 votes
public Product getProductDetails(int productId) {

		Session session = this.sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();

		String query = "from Product where productId=:productId";
		Query query2 = session.createQuery(query);
		query2.setParameter("productId", productId);

		Object object = query2.uniqueResult();

		transaction.commit();
		session.close();

		return (Product) object;
	}
 
Example 19
Project: maven_EWorld_OpenShift   File: Update.java   View Source Code Vote up 4 votes
synchronized public static boolean updateUserEmail(String ... vals)
{
         int chk=0;
         int nor=0;
         sf=Logic.getSf();
         Session s = null;
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("update UserDetails ud set ud.email_id=:email where ud.usr_id=:uid");
                qry.setParameter("uid", vals[0]);
                qry.setParameter("email", vals[1]);
                nor=qry.executeUpdate();
                System.out.println("No. Of rows updated="+nor);
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                         System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                if(nor!=0) 
                {
                     return (true);
                }
                else
                {
                     return (false);
                }
            }
            else
            {
                 return (false);
            }
}
 
Example 20
Project: Celebino   File: GardenDao.java   View Source Code Vote up 3 votes
/**
 * get garden by username
 * 
 * @param email
 * @return
 * @throws SQLException
 */
public List<Garden> getByUsername(String username) throws SQLException {

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

	List<Garden> gardens = null;

	try {

		String hql = "from Garden as g" + " where g.user.username like :username";

		Query query = session.createQuery(hql);
		query.setParameter("username", "%" + username + "%");

		gardens = (List<Garden>) query.list();

	} catch (HibernateException hibernateException) {

		session.getTransaction().rollback();

		throw new SQLException(hibernateException);

	} finally {

		session.close();
	}

	return gardens;
}