Example usage for org.apache.ibatis.session SqlSession close

List of usage examples for org.apache.ibatis.session SqlSession close

Introduction

In this page you can find the example usage for org.apache.ibatis.session SqlSession close.

Prototype

@Override
void close();

Source Link

Document

Closes the session.

Usage

From source file:com.gordcorp.jira2db.persistence.MyBatisDao.java

License:Open Source License

/**
 * Method updates the object by id. </br></br> You will usually override
 * this method. But it can be used for simple objects. </br> Example: </br>
 * If your DAO object is called CarInfo.java, the corresponding mapper query
 * id should be: &lt;update id="updateCarInfo" ... </br></br> SQL Executed
 * (example): update [tablename] set fieldname1 = value1 where id = #{id}
 * //from w ww . j av a2 s . c  o  m
 */
@Override
public int update(T o) throws PersistenceException {
    SqlSession session = sf.openSession();
    int status = 0;
    try {
        String query = NAMESPACE + "." + PREFIX_UPDATE_QUERY + o.getClass().getSimpleName();
        status = session.update(query, o);
        session.commit();
    } finally {
        session.close();
    }
    return status;
}

From source file:com.gordcorp.jira2db.persistence.MyBatisDao.java

License:Open Source License

/**
 * Method deletes the object by id. </br></br> Example: </br> If your DAO
 * object is called CarInfo.java, the corresponding mapper query id should
 * be: &lt;delete id="deleteCarInfo" ... </br></br> SQL Executed (example):
 * update [tablename] set fieldname1 = value1 where id = #{id}
 * /*from ww w . ja  va2s.  c o m*/
 */
@Override
public int delete(PK id) throws PersistenceException {
    SqlSession session = sf.openSession();
    int status = 0;
    try {
        String query = NAMESPACE + "." + PREFIX_DELETE_QUERY + this.type.getSimpleName();
        status = session.delete(query, id);
        session.commit();
    } finally {
        session.close();
    }
    return status;
}

From source file:com.healthcaresolutions.hisif.datamanager.StaffManager.java

public Staff setStaffInfo(String code, String name, String lastName, String license) {
    Staff result = null;/*  www .ja  va2s. co  m*/
    try {
        SqlSession session = SessionFactoryHelper.getSqlSessionFactory().openSession();
        StaffMapper staffMapper = session.getMapper(StaffMapper.class);
        StaffExample example = new StaffExample();
        example.createCriteria().andStaffCodeEqualTo(code);

        List<Staff> staffs = staffMapper.selectByExample(example);
        if (staffs.size() > 0) {
            result = staffs.get(0);
            result.setStaffFname(name);
            result.setStaffLname(lastName);
            result.setStaffLicense(license);
            staffMapper.updateByPrimaryKeySelective(result);
            session.commit();
        } else {
            result = new Staff();
            result.setRoleId(0);
            result.setStaffCode(code);
            result.setStaffFname(name);
            result.setStaffLname(lastName);
            result.setStaffLicense(license);
            result.setStaffMail("");
            result.setStaffMobile("");
            result.setStaffPwd("");
            result.setStaffUsr(code);
            result.setStaffTel("");
            result.setStaffTitle("");
            result.setTrId(0);
            staffMapper.insertSelective(result);
            session.commit();
        }
        session.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return result;
}

From source file:com.holacampus.api.security.BasicAuthenticator.java

License:Open Source License

@Override
public final UserPrincipal authenticate(MultivaluedMap<String, String> headers)
        throws AuthenticationFailException, AuthenticationBadSintaxException {
    String authHeader = null;/*from  w  ww .  j  a  v a 2 s  .  com*/
    UserPrincipal up;
    /*
     * If authentication header is not present, return 401 error
    */
    if ((authHeader = headers.getFirst(AUTHENTICATION_HEADER)) == null) {
        throw new AuthenticationFailException();
    }

    /*
     * Get user credentials from Authentication header
    */
    ProvidedCredentials providedCredentials = getUserCredentials(authHeader);

    /*
     * Retrieve credentials from database
    */
    logger.info("[AUTH] Credentials: " + providedCredentials.email + ":" + providedCredentials.password);

    SqlSession session = MyBatisConnectionFactory.getSession().openSession();

    try {
        CredentialsMapper credMapper = session.getMapper(CredentialsMapper.class);

        /*
        * Get Credentials from database and compare
        */
        Credentials credentials = credMapper.getCredentialsForEmail(providedCredentials.email);

        if (credentials != null && PasswordHash.validatePassword(providedCredentials.password, credentials)) {
            up = new UserPrincipal(providedCredentials.email, credentials.getElement().getId(), null);
        } else {
            throw new AuthenticationFailException();
        }

    } catch (AuthenticationFailException e) {
        throw e;
    } catch (Exception ex) {
        logger.error(ex);
        throw new InternalServerErrorException();
    } finally {
        session.close();
    }

    return up;
}

From source file:com.holacampus.api.security.TokenAuthenticator.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w ww .  jav a  2  s . co m*/
 */
@Override
public UserPrincipal authenticate(MultivaluedMap<String, String> headers)
        throws AuthenticationFailException, AuthenticationBadSintaxException {
    UserPrincipal up;
    String token = null;

    /*
    * If authentication header is not present, return 401 error
    */
    if ((token = headers.getFirst(AUTHENTICATION_HEADER)) == null) {
        throw new AuthenticationFailException();
    }

    AuthToken auth;
    /*
     * Check token with the one stored on the database
     */
    SqlSession session = MyBatisConnectionFactory.getSession().openSession();

    try {
        AuthTokenMapper authMapper = session.getMapper(AuthTokenMapper.class);

        /*
        * Get Token from database and compare
        */
        auth = authMapper.getAuthTokenAndCredentials(token);

        if (auth != null) {
            up = new UserPrincipal(auth.getElement().getEmail(), auth.getElement().getId(),
                    auth.getElement().getType());
        } else {
            throw new AuthenticationFailException();
        }

    } catch (AuthenticationFailException e) {
        throw e;
    } catch (Exception ex) {
        logger.error(ex);
        throw new InternalServerErrorException();
    } finally {
        session.close();
    }

    return up;
}

From source file:com.inform.project.dao.MyBatisAdminImpl.java

@Override
public LoginAuthModel getUser(String login, String psw) {
    LoginAuthModel sys = null;//from  w ww.j a v  a 2 s. c  o  m
    SqlSession session = null;
    try {
        session = MyBatisSession.getInst().getSession().openSession();
        sys = session.selectOne("GetUsersMapper.getUser", new LoginAuthModel(login, psw));
    } catch (IOException ex) {
        Logger.getLogger(MyBatisAdminImpl.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return sys;
}

From source file:com.inform.project.dao.MyBatisAdminImpl.java

@Override
public List<LoginAuthModel> getList() {
    List<LoginAuthModel> list = null;
    SqlSession session = null;
    try {/*from  w ww . j a v a 2  s  .  c o  m*/
        session = MyBatisSession.getInst().getSession().openSession();
        list = session.selectList("GetUsersMapper.getList");
    } catch (IOException ex) {
        Logger.getLogger(MyBatisAdminImpl.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return list;
}

From source file:com.inform.project.dao.MyBatisAdminImpl.java

@Override
public void deleteObject(int id) {
    SqlSession session = null;
    try {//from w  w w.j ava 2  s  .co m
        session = MyBatisSession.getInst().getSession().openSession();
        session.delete("GetUsersMapper.deleteOne", id);
        session.commit();
    } catch (IOException ex) {
        Logger.getLogger(MyBatisAdminImpl.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (session != null) {
            session.close();
        }
    }

}

From source file:com.inform.project.dao.MyBatisAdminImpl.java

@Override
public void addObject(LoginAuthModel user) {
    SqlSession session = null;
    try {/*from  w ww . j  av a  2s  .c om*/
        session = MyBatisSession.getInst().getSession().openSession();
        session.insert("GetUsersMapper.insertOne", user);
        session.commit();
    } catch (IOException ex) {
        Logger.getLogger(MyBatisAdminImpl.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:com.inform.project.dao.MyBatisAdminImpl.java

@Override
public void updateUser(LoginAuthModel user) {
    SqlSession session = null;
    try {//w  w  w.j a  va 2  s  .  c  o m
        session = MyBatisSession.getInst().getSession().openSession();
        session.update("GetUsersMapper.updateOne", user);
        session.commit();
        System.out.println("commit");
    } catch (IOException ex) {
        System.out.println(ex);
        Logger.getLogger(MyBatisAdminImpl.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}