public void updateAdmin(Admin admin) throws DBException { Session session; Transaction tx = null; try { session = HibernateSessionFactory.currentSession(); tx = session.beginTransaction(); session.update(admin); tx.commit(); } catch (HibernateException e) { try { tx.rollback(); } catch (HibernateException e1) { e1.printStackTrace(); } e.printStackTrace(); throw new DBException("获取对象失败!"); } finally { try { HibernateSessionFactory.closeSession(); } catch (HibernateException e) { e.printStackTrace(); } } }
public void deleteAdmin(Long id) throws DBException { Session session; Admin admin = null; Transaction tx = null; try { session = HibernateSessionFactory.currentSession(); tx = session.beginTransaction(); admin = (Admin) session.load(Admin.class, id); session.delete(admin); tx.commit(); } catch (HibernateException e) { try { tx.rollback(); } catch (HibernateException e1) { e1.printStackTrace(); } e.printStackTrace(); throw new DBException("更新对象失败!"); } finally { try { HibernateSessionFactory.closeSession(); } catch (HibernateException e) { e.printStackTrace(); } } }
public static void main(String[] args) throws HibernateException { Person person = new Person(); person.setName("John Smith"); person.setEmail("*****@*****.**"); Session session = _sessions.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(person); tx.commit(); } catch (HibernateException he) { if (tx != null) tx.rollback(); throw he; } finally { session.close(); } }
public void rollback() throws RetsServerException { LOG.debug("rollback"); if (mTx != null) { try { Transaction tx = mTx; mTx = null; tx.rollback(); } catch (HibernateException e) { throw new RetsServerException(e); } } }
/** * Updates a class in database * * @param c IClass to update * @throws HibernateException */ public static void update(IClass c) throws HibernateException { Session s = null; Transaction tx = null; try { s = HibernateUtil.currentSession(); tx = s.beginTransaction(); s.update(c); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); if (tx != null) tx.rollback(); throw e; } finally { HibernateUtil.closeSession(); } }
/** * @param principal the user principal representing the desired user * @param name the name of the workflow to create. * @param actionId the ID of the initial action * @param inputs the inputs to the workflow * @return a WorkflowVO representing the newly created workflow instance * @throws SystemException if an error occurs while initiaizing the workflow */ public WorkflowVO initializeWorkflow( InfoGluePrincipal principal, String name, int actionId, Map inputs) throws SystemException { WorkflowVO workflowVO = null; try { Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); if (getIsAccessApproved(name, principal)) { WorkflowFacade wf = new WorkflowFacade( principal, name, actionId, inputs, hibernateSessionFactory, session); workflowVO = wf.createWorkflowVO(); session.flush(); tx.commit(); } else { throw new Bug("You are not allowed to create " + name + " workflows."); } } catch (Exception e) { logger.error( "An error occurred when we tries to run initializeWorkflow():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction:" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close:" + e.getMessage(), e); } } } catch (Exception e) { throw new SystemException(e); } return workflowVO; }
/** * Returns a list of all available workflows, i.e., workflows defined in workflows.xml * * @param userPrincipal a user principal * @return a list WorkflowVOs representing available workflows */ public List<WorkflowVO> getAvailableWorkflowVOList(InfoGluePrincipal userPrincipal) throws SystemException { final List<WorkflowVO> accessibleWorkflows = new ArrayList<WorkflowVO>(); Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); WorkflowFacade wf = new WorkflowFacade(userPrincipal, hibernateSessionFactory, session); final List<WorkflowVO> allWorkflows = wf.getDeclaredWorkflows(); for (final Iterator<WorkflowVO> i = allWorkflows.iterator(); i.hasNext(); ) { final WorkflowVO workflowVO = i.next(); if (getIsAccessApproved(workflowVO.getName(), userPrincipal)) { accessibleWorkflows.add(workflowVO); } } session.flush(); tx.commit(); } catch (Exception e) { logger.error( "An error occurred when we tries to execute getAvailableWorkflowVOList():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction():" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close session:" + e.getMessage(), e); } } return accessibleWorkflows; }
/** * Rollback all the changes after last commit. Declared in AbstractDAO class. * * @throws DAOException */ public void rollback() throws DAOException { // Logger.out.info("rollback call:------------------------"); try { Logger.out.debug("SESSION COMMIT: " + session.connection().getAutoCommit()); } catch (Exception dbex) { dbex.printStackTrace(); } if (isUpdated == true) { try { if (transaction != null) transaction.rollback(); } catch (HibernateException dbex) { Logger.out.error(dbex.getMessage(), dbex); throw handleError("Error in rollback: ", dbex); } } }
/** * Method to get all the classes * * @param parent * @return List of IClass * @throws HibernateException */ public static List<IClass> get(ITrack parent) throws HibernateException { Session s = null; Transaction tx = null; List<IClass> l = null; try { s = HibernateUtil.currentSession(); tx = s.beginTransaction(); l = s.find("FROM ClassImpl as c WHERE c.parentTrack = ?", parent.getIdData(), Hibernate.LONG); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); if (tx != null) tx.rollback(); throw e; } finally { HibernateUtil.closeSession(); } return l; }
public Map getProperties(InfoGluePrincipal userPrincipal, long workflowId) { if (logger.isDebugEnabled()) { logger.info("userPrincipal:" + userPrincipal); logger.info("workflowId:" + workflowId); } Map parameters = new HashMap(); Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); PropertySet propertySet = getPropertySet(userPrincipal, workflowId, session); for (Iterator keys = propertySet.getKeys().iterator(); keys.hasNext(); ) { String key = (String) keys.next(); parameters.put(key, propertySet.getString(key)); } session.flush(); tx.commit(); } catch (Exception e) { logger.error("An error occurred when we tries to run getHistorySteps():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction:" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close session:" + e.getMessage(), e); } } return parameters; }
/** * Returns the workflows owned by the specified principal. * * @param userPrincipal a user principal. * @return a list of WorkflowVOs owned by the principal. * @throws SystemException if an error occurs while finding the workflows */ public List getMyCurrentWorkflowVOList(InfoGluePrincipal userPrincipal) throws SystemException { List list = new ArrayList(); Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); WorkflowFacade wf = new WorkflowFacade(userPrincipal, hibernateSessionFactory, session); list = wf.getMyActiveWorkflows(userPrincipal); session.flush(); tx.commit(); } catch (Exception e) { logger.error( "An error occurred when we tries to execute getMyCurrentWorkflowVOList():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction:" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close session:" + e.getMessage(), e); } } return list; // return new WorkflowFacade(userPrincipal, true).getMyActiveWorkflows(userPrincipal); }
public static IClass getClassFromId(Long idClass) throws HibernateException { IClass c = null; Session s = null; Transaction tx = null; try { s = HibernateUtil.currentSession(); tx = s.beginTransaction(); c = (IClass) s.load(ClassImpl.class, idClass); tx.commit(); System.out.println(c); } catch (HibernateException e) { e.printStackTrace(); if (tx != null) tx.rollback(); throw e; } finally { HibernateUtil.closeSession(); } return c; }
/** * Returns all current steps for a workflow, i.e., steps that could be performed in the workflow's * current state * * @param userPrincipal a user principal * @param workflowId the Id of the desired workflow * @return a list of WorkflowStepVOs representing the current steps of the workflow with * workflowId */ public List getCurrentSteps(InfoGluePrincipal userPrincipal, long workflowId) { List currentSteps = new ArrayList(); Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); WorkflowFacade wf = new WorkflowFacade(userPrincipal, workflowId, hibernateSessionFactory, session); currentSteps = wf.getCurrentSteps(); session.flush(); tx.commit(); } catch (Exception e) { logger.error("An error occurred when we tries to run getCurrentSteps():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction:" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close session:" + e.getMessage(), e); } } // WorkflowFacade wf = new WorkflowFacade(userPrincipal, workflowId, true); // List currentSteps = wf.getCurrentSteps(); return currentSteps; }
private void parseMetadata() throws Exception { Session hSession = null; Transaction tx = null; try { hSession = mSessions.openSession(); tx = hSession.beginTransaction(); MSystem hSystem = doSystem(hSession); doResource(hSession, hSystem); doClasses(hSession); doObjects(hSession); doSearchHelp(hSession); doEditMask(hSession); doLookup(hSession); doLookupTypes(hSession); doUpdateHelp(hSession); doValidationExternal(hSession); doUpdate(hSession); doTable(hSession); doValidationLookup(hSession); doValidationLookupType(hSession); doValidationExternalType(hSession); doValidationExpression(hSession); doUpdateType(hSession); doForeignKey(hSession, hSystem); tx.commit(); hSession.close(); } catch (Exception e) { e.printStackTrace(); try { tx.rollback(); hSession.close(); } catch (Exception e2) { e2.printStackTrace(); } } }
/** * Returns true if the workflow has terminated; false otherwise. * * @param workflowVO the workflow. * @return true if the workflow has terminated; false otherwise. */ public boolean hasTerminated(InfoGluePrincipal userPrincipal, long workflowId) throws WorkflowException { boolean isFinished = false; Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); WorkflowFacade wf = new WorkflowFacade(userPrincipal, workflowId, hibernateSessionFactory, session); isFinished = wf.isFinished(); session.flush(); tx.commit(); } catch (Exception e) { logger.error("An error occurred when we tries to run hasTerminated:" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction:" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close session:" + e.getMessage(), e); } } return isFinished; // return new WorkflowFacade(userPrincipal, workflowId, true).isFinished(); }
public PropertySet getPropertySet(InfoGluePrincipal userPrincipal, long workflowId) { PropertySet propertySet = null; Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); WorkflowFacade wf = new WorkflowFacade(userPrincipal, workflowId, hibernateSessionFactory, session); propertySet = wf.getPropertySet(); session.flush(); tx.commit(); } catch (Exception e) { logger.error("An error occurred when we tries to run getHistorySteps():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction:" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close session:" + e.getMessage(), e); } } return propertySet; // return new WorkflowFacade(userPrincipal, workflowId, false).getPropertySet(); }
/** * Invokes an action on a workflow for a given user and request * * @param principal the user principal * @param workflowId the ID of the desired workflow * @param actionId the ID of the desired action * @param inputs the inputs to the workflow * @return a WorkflowVO representing the current state of the workflow identified by workflowId * @throws WorkflowException if a workflow error occurs */ public WorkflowVO invokeAction( InfoGluePrincipal principal, long workflowId, int actionId, Map inputs) throws WorkflowException { WorkflowVO workflowVO = null; Session session = null; net.sf.hibernate.Transaction tx = null; try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); WorkflowFacade wf = new WorkflowFacade(principal, workflowId, hibernateSessionFactory, session); wf.doAction(actionId, inputs); session.flush(); tx.commit(); } catch (Exception e) { logger.error( "An error occurred when we tries to execute invokeAction():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction:" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close session:" + e.getMessage(), e); } } try { session = hibernateSessionFactory.openSession(); tx = session.beginTransaction(); WorkflowFacade wf = new WorkflowFacade(principal, workflowId, hibernateSessionFactory, session); workflowVO = wf.createWorkflowVO(); session.flush(); tx.commit(); } catch (Exception e) { logger.error( "An error occurred when we tries to execute invokeAction():" + e.getMessage(), e); try { tx.rollback(); } catch (HibernateException he) { logger.error( "An error occurred when we tries to rollback transaction:" + he.getMessage(), he); } restoreSessionFactory(e); } finally { try { session.close(); } catch (HibernateException e) { logger.error("An error occurred when we tries to close session:" + e.getMessage(), e); } } return workflowVO; }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { Integer toId; // to user id Integer fromId; // from user id Integer itemId; // item id String comment; // user comment Integer rating; // user rating ServletPrinter sp = null; Session sess = null; Transaction trans = null; sp = new ServletPrinter(response, "StoreComment"); /* Get and check all parameters */ String toStr = request.getParameter("to"); if (toStr == null || toStr.equals("")) { printError("<h3>You must provide a 'to user' identifier !<br></h3>", sp); return; } toId = new Integer(toStr); String fromStr = request.getParameter("from"); if (fromStr == null || fromStr.equals("")) { printError("<h3>You must provide a 'from user' identifier !<br></h3>", sp); return; } fromId = new Integer(fromStr); String itemStr = request.getParameter("itemId"); if (itemStr == null || itemStr.equals("")) { printError("<h3>You must provide an item identifier !<br></h3>", sp); return; } itemId = new Integer(itemStr); String ratingStr = request.getParameter("rating"); if (ratingStr == null || ratingStr.equals("")) { printError("<h3>You must provide a rating !<br></h3>", sp); return; } rating = new Integer(ratingStr); comment = request.getParameter("comment"); if (comment == null || comment.equals("")) { printError("<h3>You must provide a comment !<br></h3>", sp); return; } try { sess = getSession(); trans = sess.beginTransaction(); User toUser = (User) sess.load(User.class, toId); User fromUser = (User) sess.load(User.class, fromId); Item item = (Item) sess.load(Item.class, itemId); /* if (toUser == null) { try { trans.rollback(); } catch (HibernateException he) { printError("Transaction rollback failed: " + he + "<br>", sp); } printError("This user does not exist in the database.", sp); releaseSession(sess); return; } if (fromUser == null) { try { trans.rollback(); } catch (HibernateException he) { printError("Transaction rollback failed: " + he + "<br>", sp); } printError("This user does not exist in the database.", sp); releaseSession(sess); return; } if (item == null) { try { trans.rollback(); } catch (HibernateException he) { printError("Transaction rollback failed: " + he + "<br>", sp); } printError("This item does not exist in the database.", sp); releaseSession(sess); return; } */ Date now = new Date(); // Try to create a new comment Comment newComment = new Comment(); newComment.setFromUser(fromUser); newComment.setToUser(toUser); newComment.setItem(item); newComment.setRating(rating); newComment.setDate(now); newComment.setComment(comment); sess.save(newComment); if (Hibernate.isInitialized(toUser) && Hibernate.isInitialized(toUser.getCommentsTo())) { toUser.getCommentsTo().add(newComment); } if (Hibernate.isInitialized(fromUser) && Hibernate.isInitialized(fromUser.getCommentsFrom())) { fromUser.getCommentsFrom().add(newComment); } if (Hibernate.isInitialized(item) && Hibernate.isInitialized(item.getComments())) { item.getComments().add(newComment); } toUser.setRating(new Integer(toUser.getRating().intValue() + rating.intValue())); sp.printHTMLheader("RUBiS: Comment posting"); sp.printHTML("<center><h2>Your comment has been successfully posted.</h2></center>"); trans.commit(); releaseSession(sess); } catch (Exception e) { printError("Error adding new comment: " + e + ")<br>", sp); try { trans.rollback(); } catch (HibernateException he) { sp.printHTML("Transaction rollback failed: " + he + "<br>"); } releaseSession(sess); return; } sp.printHTMLfooter(); }
public void testCreateCustomer() throws Exception { if (logger.isDebugEnabled()) { logger.debug("testCreateCustomer() - start"); } // get new Session and begin Transaction Session s = HibernateUtil.currentSession(); Transaction tx = null; try { tx = s.beginTransaction(); try { // is DB open and connected assertTrue("Connected to Db? ", s.isConnected()); assertTrue("Db Open? ", s.isOpen()); List data = new ArrayList(); List customer = new ArrayList(); // create new objects Customer c = new Customer(); c.setUserName("kunde1"); c.setUserPassword("kunde1"); c.setHiddenQuestion(" "); c.setHiddenAnswer(" "); Person person = new Person(); person.setFirstName("Philipp"); person.setLastName("Schneider"); person.setStreet("Kastanienring"); person.setStreetNumber("16"); person.setCity("Leipzig"); person.setZipCode("04316"); person.setCountry("Deutschland"); person.setSalutation("Herr"); person.setEmailAddress("*****@*****.**"); Calendar birthday = Calendar.getInstance(); birthday.set(Calendar.DAY_OF_MONTH, 10); birthday.set(Calendar.MONTH, 8); birthday.set(Calendar.YEAR, 1988); person.setDayOfBirth(birthday); c.setPerson(person); c.setDayOfRegistration(Calendar.getInstance()); customer.add(c); // second Customer c = new Customer(); c.setUserName("kunde2"); c.setUserPassword("kunde2"); c.setHiddenQuestion(" "); c.setHiddenAnswer(" "); person = new Person(); person.setFirstName("Max"); person.setLastName("Meier"); person.setStreet("Kohlgartenstr."); person.setStreetNumber("67"); person.setCity("Wernigerode"); person.setZipCode("38855"); person.setCountry("Deutschland"); person.setSalutation("Herr"); person.setEmailAddress("*****@*****.**"); birthday = Calendar.getInstance(); birthday.set(Calendar.DAY_OF_MONTH, 2); birthday.set(Calendar.MONTH, 2); birthday.set(Calendar.YEAR, 1954); person.setDayOfBirth(birthday); c.setPerson(person); c.setDayOfRegistration(Calendar.getInstance()); customer.add(c); // third Customer c = new Customer(); c.setUserName("kunde3"); c.setUserPassword("kunde3"); c.setHiddenQuestion(" "); c.setHiddenAnswer(" "); person = new Person(); person.setFirstName("Thomas"); person.setLastName("Pech"); person.setStreet("Riebeck."); person.setStreetNumber("7a"); person.setCity("Leipzig"); person.setZipCode("04317"); person.setCountry("Deutschland"); person.setSalutation("Herr"); person.setEmailAddress("*****@*****.**"); birthday = Calendar.getInstance(); birthday.set(Calendar.DAY_OF_MONTH, 10); birthday.set(Calendar.MONTH, 8); birthday.set(Calendar.YEAR, 1988); person.setDayOfBirth(birthday); c.setDayOfRegistration(Calendar.getInstance()); c.setPerson(person); customer.add(c); List ids = new ArrayList(); // save objects for (Iterator iter = customer.iterator(); iter.hasNext(); ) { Customer custe = (Customer) iter.next(); ids.add((Integer) s.save(custe)); } s.flush(); for (int i = 0; i < ids.size(); i++) { int id = ((Integer) ids.get(i)).intValue(); // get Condition from Hibernate Customer dbc = (Customer) s.get(Customer.class, new Integer(id)); assertNotNull("Can't get Customer" + id + " from DB", dbc); if (dbc == null) { if (logger.isDebugEnabled()) { logger.debug("testCreateCustomer() Kein object mit id " + id + "gefunden."); } return; } logger.debug("Customer: " + dbc + " Person:" + dbc.getPerson()); Customer myc = (Customer) customer.get(i); // are both equal? assertEquals( "Select: Customer aus DB nicht gleich meiner. DB: " + dbc + " My:" + myc, myc, dbc); } tx.commit(); } catch (Exception e) { if (tx != null) { logger.error( "testCreateCustomer() - Something went wrong here; discard all partial changes", e); // Something went wrong; discard all partial changes tx.rollback(); } } } catch (Exception e) { logger.error("testCreateCustomer() - Error while trying to beginTransaction", e); throw e; } finally { // No matter what, close the session HibernateUtil.closeSession(); } if (logger.isDebugEnabled()) { logger.debug("testCreateCustomer() - end"); } }