Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java hibernate函数编码_Java_Hibernate - Fatal编程技术网

Java hibernate函数编码

Java hibernate函数编码,java,hibernate,Java,Hibernate,我有以下场景,authenticateUser()调用saveApplicationLog() 我想使整个操作原子化,但不想合并这两个函数,因为其他函数也使用saveApplicationLog()。做这件事的正确方法是什么?如果您使用in方法管理事务,那么您就不能完成整个原子操作。 您必须将事务管理代码移动到另一个方法,例如execute(),并从execute()调用authenticateUser()和saveApplicationLog()。您必须对这两个操作使用相同的会话。如果我将au

我有以下场景,
authenticateUser()
调用
saveApplicationLog()


我想使整个操作原子化,但不想合并这两个函数,因为其他函数也使用
saveApplicationLog()
。做这件事的正确方法是什么?

如果您使用in方法管理事务,那么您就不能完成整个原子操作。
您必须将事务管理代码移动到另一个方法,例如
execute()
,并从
execute()
调用
authenticateUser()
saveApplicationLog()
。您必须对这两个操作使用相同的会话。

如果我将
authenticateUser()
会话传递给
saveApplicationLog()
它会工作吗?是的,如果您的事务管理代码只在authenticateUser()中,那么它会工作吗
private boolean authenticateUser(String username, String password) {

    Session session = GCCPersistenceMangerFactory.getSessionFactory().openSession();
    Transaction tx = null;
    Boolean found = false;
    try{
        tx = session.beginTransaction();
        String query = "FROM PersistentUserMasterData where userName = '" + username+"',password = '"+password+"'"; 
        Query q = session.createQuery(query);
        List <PersistentUserMasterData> userInfo = new ArrayList<PersistentUserMasterData> (q.list()); 
        if (userInfo != null ) {
            for(PersistentUserMasterData user : userInfo) {
                found = true;
            }
        }

        tx.commit();
    }catch (HibernateException e) {
        if (tx!=null) tx.rollback();
        e.printStackTrace(); 
    }finally {
        session.close(); 
    }

    if(found){
        ApplicationLogsDTO logsDto = new ApplicationLogsDTO(
                "Login was succesfull for LDAP user: "+ username,
                "LDAP_LOGIN_SUCCESS", "LOGIN", username, Severity.INFO.ordinal());

        saveApplicationLog(logsDto);//function 2
    }
    return found; 
}
public void saveApplicationLog(ApplicationLogsDTO log){

    PersistentApplicationLogs pLog = new PersistentApplicationLogs(
            log.getDetailDescription(), log.getEvent(), log.getEventSource(),log.getUser(), log.getSeverity(),getThreadLocalRequest().getRemoteAddr());
    Session session = GCCPersistenceMangerFactory.getSessionFactory().openSession();
    Transaction tx = null;
    try{
        tx = session.beginTransaction();
        session.save(pLog);
        tx.commit();
    }catch (HibernateException e) {
        if (tx!=null) tx.rollback();
        e.printStackTrace();
    }finally {
        session.close(); 
    }

}