001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.core.persistence;
021
022import org.apache.ibatis.executor.BatchResult;
023import org.apache.ibatis.session.Configuration;
024import org.apache.ibatis.session.ResultHandler;
025import org.apache.ibatis.session.RowBounds;
026import org.apache.ibatis.session.SqlSession;
027
028import java.sql.Connection;
029import java.util.List;
030import java.util.Map;
031
032public final class BatchSession implements SqlSession {
033
034  public static final int MAX_BATCH_SIZE = 250;
035
036  private final SqlSession session;
037  private final int batchSize;
038  private int count = 0;
039
040  BatchSession(SqlSession session) {
041    this(session, MAX_BATCH_SIZE);
042  }
043
044  BatchSession(SqlSession session, int batchSize) {
045    this.session = session;
046    this.batchSize = batchSize;
047  }
048
049  public void select(String statement, Object parameter, ResultHandler handler) {
050    reset();
051    session.select(statement, parameter, handler);
052  }
053
054  public void select(String statement, ResultHandler handler) {
055    reset();
056    session.select(statement, handler);
057  }
058
059  public Object selectOne(String statement) {
060    reset();
061    return session.selectOne(statement);
062  }
063
064  public Object selectOne(String statement, Object parameter) {
065    reset();
066    return session.selectOne(statement, parameter);
067  }
068
069  public List selectList(String statement) {
070    reset();
071    return session.selectList(statement);
072  }
073
074  public List selectList(String statement, Object parameter) {
075    reset();
076    return session.selectList(statement, parameter);
077  }
078
079  public List selectList(String statement, Object parameter, RowBounds rowBounds) {
080    reset();
081    return session.selectList(statement, parameter, rowBounds);
082  }
083
084  public Map selectMap(String statement, String mapKey) {
085    reset();
086    return session.selectMap(statement, mapKey);
087  }
088
089  public Map selectMap(String statement, Object parameter, String mapKey) {
090    reset();
091    return session.selectMap(statement, parameter, mapKey);
092  }
093
094  public Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
095    reset();
096    return session.selectMap(statement, parameter, mapKey, rowBounds);
097  }
098
099  public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
100    reset();
101    session.select(statement, parameter, rowBounds, handler);
102  }
103
104  public int insert(String statement) {
105    increment();
106    return session.insert(statement);
107  }
108
109  public int insert(String statement, Object parameter) {
110    increment();
111    return session.insert(statement, parameter);
112  }
113
114  public int update(String statement) {
115    increment();
116    return session.update(statement);
117  }
118
119  public int update(String statement, Object parameter) {
120    increment();
121    return session.update(statement, parameter);
122  }
123
124  public int delete(String statement) {
125    increment();
126    return session.delete(statement);
127  }
128
129  public int delete(String statement, Object parameter) {
130    increment();
131    return session.delete(statement, parameter);
132  }
133
134  public void commit() {
135    session.commit();
136    reset();
137  }
138
139  public void commit(boolean force) {
140    session.commit(force);
141    reset();
142  }
143
144  public void rollback() {
145    session.rollback();
146    reset();
147  }
148
149  public void rollback(boolean force) {
150    session.rollback(force);
151    reset();
152  }
153
154  public List<BatchResult> flushStatements() {
155    List<BatchResult> batchResults = session.flushStatements();
156    reset();
157    return batchResults;
158  }
159
160  public void close() {
161    session.close();
162  }
163
164  public void clearCache() {
165    session.clearCache();
166  }
167
168  public Configuration getConfiguration() {
169    return session.getConfiguration();
170  }
171
172  public <T> T getMapper(Class<T> type) {
173    return getConfiguration().getMapper(type, this);
174  }
175
176  public Connection getConnection() {
177    return session.getConnection();
178  }
179
180  private BatchSession increment() {
181    count += 1;
182    if (count >= batchSize) {
183      commit();
184    }
185    return this;
186  }
187
188  private void reset() {
189    count = 0;
190  }
191}