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.google.enterprise.connector.db.DBConnectorTypeTest.java

License:Apache License

/**
 * Executes the given query and applies the given function to the
 * result set.//from  w w w  . ja v  a 2s  .c o m
 *
 * @return the value of the function applied to the {@code ResultSet}
 */
private <T> T applyResultSet(String sqlQuery, DBClient.SqlFunction<ResultSet, T> function) throws SQLException {
    SqlSession sqlSession = getDbClient().getSqlSession();
    try {
        Connection dbConnection = sqlSession.getConnection();
        try {
            Statement stmt = dbConnection.createStatement();
            try {
                ResultSet resultSet = stmt.executeQuery(sqlQuery);
                try {
                    return function.apply(resultSet);
                } finally {
                    resultSet.close();
                }
            } finally {
                stmt.close();
            }
        } finally {
            dbConnection.close();
        }
    } finally {
        sqlSession.close();
    }
}

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

License:Open Source License

public List<JiraCustomFieldDto> getAllByJiraKey(String key) throws PersistenceException {

    SqlSession session = sf.openSession();
    List<JiraCustomFieldDto> obj = new ArrayList<JiraCustomFieldDto>();
    try {/*from w w  w. jav  a2 s.  c o m*/
        String query = NAMESPACE + "." + PREFIX_SELECT_QUERY + this.type.getSimpleName() + "ByJiraKey";
        List<Object> objects = session.selectList(query, key);
        for (Object o : objects) {
            obj.add((JiraCustomFieldDto) o);
        }
    } finally {
        session.close();
    }
    return obj;
}

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

License:Open Source License

public int deleteAllByJiraKey(String jiraKey) throws PersistenceException {
    SqlSession session = sf.openSession();
    int status = 0;
    try {//from w ww  .  j  av  a 2 s .  co  m
        String query = NAMESPACE + "." + PREFIX_DELETE_QUERY + this.type.getSimpleName() + "ByJiraKey";
        status = session.delete(query, jiraKey);
        session.commit();
    } finally {
        session.close();
    }
    return status;
}

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

License:Open Source License

public JiraIssueDto getByJiraKey(String key) throws PersistenceException {

    SqlSession session = sf.openSession();
    JiraIssueDto obj = null;/*from  ww w.  j  ava2s . c  o m*/
    try {
        String query = NAMESPACE + "." + PREFIX_SELECT_QUERY + this.type.getSimpleName() + "ByJiraKey";
        obj = (JiraIssueDto) session.selectOne(query, key);
    } finally {
        session.close();
    }
    return obj;
}

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

License:Open Source License

public int deleteByJiraKey(String jiraKey) throws PersistenceException {
    SqlSession session = sf.openSession();
    int status = 0;
    try {/*from w w  w  . jav  a  2 s. c  o  m*/
        String query = NAMESPACE + "." + PREFIX_DELETE_QUERY + this.type.getSimpleName() + "ByJiraKey";
        status = session.delete(query, jiraKey);
        session.commit();
    } finally {
        session.close();
    }
    return status;
}

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

License:Open Source License

public int updateByJiraKey(JiraIssueDto jiraIssueDto) {
    SqlSession session = sf.openSession();
    int status = 0;
    try {//from w w  w. j  a  v a2  s  . c  o  m
        String query = NAMESPACE + "." + PREFIX_UPDATE_QUERY + this.type.getSimpleName() + "ByJiraKey";
        status = session.update(query, jiraIssueDto);
        session.commit();
    } finally {
        session.close();
    }
    return status;
}

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

License:Open Source License

/**
 * Default get by id method. </br></br> Almost all objects in the db will
 * need this (except mapping tables for multiple joins, which you probably
 * shouldn't even have as objects in your model, since proper MyBatis
 * mappings can take care of that). </br></br> Example: </br> If your DAO
 * object is called CarInfo.java, the corresponding mapper query id should
 * be: &lt;select id="getCarInfo" ...
 *//*ww  w.java 2s  .  c  o m*/
@Override
@SuppressWarnings("unchecked")
public T get(PK id) throws PersistenceException {

    SqlSession session = sf.openSession();
    T obj = null;
    try {
        String query = NAMESPACE + "." + PREFIX_SELECT_QUERY + this.type.getSimpleName(); // If the object's calls name
        // is AddressType.java, this
        // matches the mapper query
        // id:
        // "namespace.getAddressType"
        obj = (T) session.selectOne(query, id);
    } finally {
        session.close();
    }
    return obj;
}

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

License:Open Source License

/**
 * Method returns all rows for this object. </br></br> Example: </br> If
 * your DAO object is called CarInfo.java, the corresponding mapper query id
 * should be: &lt;select id="getAllCarInfo" ... </br></br> SQL Executed:
 * select * from [tablename] </br></br> Notes: </br> Consider overdiding
 * this method in order to handle large numbers of objects with multiple
 * references. LAZY LOADING should be enabled in this case, otherwise you
 * might run out of memory (eg. get all UserAccounts if the table has
 * 1,000,000 rows) look into the aggresiveLazyLoading property
 * *///w  w w.  j a  v  a2  s.co  m
@Override
@SuppressWarnings("unchecked")
public List<T> getAll() throws PersistenceException {

    SqlSession session = sf.openSession();
    List<T> list = null;
    try {
        String query = NAMESPACE + "." + PREFIX_SELECT_QUERY + "All" + this.type.getSimpleName();
        list = (List<T>) session.selectList(query);
    } finally {
        session.close();
    }
    return list;
}

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

License:Open Source License

/**
 * Method returns first object which matches the given name (exact match).
 * </br></br> It's up to you to decide what constitutes an object's name.
 * Typically you would have a NAME column in the table, but not all objects
 * have this. Generally this method should be overriden (if you need it at
 * all) in the child dao class. </br></br> Example: </br> If your DAO object
 * is called CarInfo.java, the corresponding mapper query id should be:
 * &lt;select id="getCarInfoByName" ... </br></br> SQL Executed (example):
 * select * from [tablename] where NAME = ?
 * //  ww w  .j  a v  a  2  s . com
 */
@SuppressWarnings("unchecked")
public T getByName(String name) throws PersistenceException {

    SqlSession session = sf.openSession();
    T obj = null;
    try {
        String query = NAMESPACE + "." + PREFIX_SELECT_QUERY + this.type.getSimpleName() + "ByName";
        obj = (T) session.selectOne(query, name);
    } finally {
        session.close();
    }
    return obj;
}

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

License:Open Source License

/**
 * Method inserts the object into the table. </br></br> You will usually
 * override this method, especially if you're inserting associated objects.
 * </br> Example: </br> If your DAO object is called CarInfo.java, the
 * corresponding mapper query id should be: &lt;insert id="createCarInfo"
 * ... </br></br> SQL Executed (example): insert into [tablename]
 * (fieldname1,fieldname2,...) values(value1,value2...) ...
 * /*  w  ww  . j av  a 2  s  .  co m*/
 */
@Override
public int create(T o) throws PersistenceException {
    SqlSession session = sf.openSession();
    int status = -1;
    try {
        String query = NAMESPACE + "." + PREFIX_INSERT_QUERY + o.getClass().getSimpleName();
        status = session.insert(query, o);
        // GenericDto genericDto = (GenericDto) o;
        // status = genericDto.getId();
        session.commit();
    } finally {
        session.close();
    }
    return status;
}