|
@@ -6,6 +6,8 @@ import org.apache.ibatis.session.ExecutorType;
|
|
|
import org.apache.ibatis.session.SqlSession;
|
|
|
import org.apache.ibatis.session.SqlSessionFactory;
|
|
|
|
|
|
+import java.sql.SQLException;
|
|
|
+import java.sql.SQLTransientConnectionException;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -24,10 +26,6 @@ public abstract class BatchDaoService {
|
|
|
this.sqlSessionFactory = sqlSessionFactory;
|
|
|
}
|
|
|
|
|
|
- public int getCount() {
|
|
|
- return this.count;
|
|
|
- }
|
|
|
-
|
|
|
public int execute(String mapperName) {
|
|
|
int total = 0;
|
|
|
SqlSession sqlSession = null;
|
|
@@ -73,7 +71,27 @@ public abstract class BatchDaoService {
|
|
|
return this.count;
|
|
|
}
|
|
|
|
|
|
- public int updateBatch(String mapperName, List<HashMap<String, Object>> lists) {
|
|
|
+ public void dbExceptionHandler(String mapperName, Exception e) {
|
|
|
+ log.error("[BATCH--DAO] {}:updateBatch: {}: Transaction exception: {}", this.serviceName, mapperName, e.getMessage());
|
|
|
+ if (e instanceof NullPointerException) {
|
|
|
+ log.error("[BATCH--DAO] {}:updateBatch: {}: Transaction exception: {}", this.serviceName, mapperName, e.getMessage());
|
|
|
+ }
|
|
|
+ else if (e instanceof SQLException) {
|
|
|
+ SQLException sqlException = (SQLException)e;
|
|
|
+ int sqlCode = sqlException.getErrorCode();
|
|
|
+ if (sqlCode == 60) {
|
|
|
+ log.error("[BATCH--DAO] {}:updateBatch: {}: Deadlock detected: {}", this.serviceName, mapperName, sqlException.getMessage());
|
|
|
+ }
|
|
|
+ else if (e instanceof SQLTransientConnectionException) {
|
|
|
+ log.error("[BATCH--DAO] {}:updateBatch: {}: Connection is not available. SQLException: {}", this.serviceName, mapperName, e.getMessage());
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ log.error("[BATCH--DAO] {}:updateBatch: {}: SQLException: {}", this.serviceName, mapperName, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public int updateBatch(String mapperName, List<HashMap<String, Object>> lists) throws Exception {
|
|
|
int total = lists.size();
|
|
|
if (total == 0) {
|
|
|
log.info("[BATCH--DAO] {}:updateBatch: {}: No Data.", this.serviceName, mapperName);
|
|
@@ -81,6 +99,7 @@ public abstract class BatchDaoService {
|
|
|
}
|
|
|
int jobCnt = 0;
|
|
|
SqlSession sqlSession = null;
|
|
|
+ boolean success = false;
|
|
|
try {
|
|
|
sqlSession = this.sqlSessionFactory.openSession(ExecutorType.BATCH, false);
|
|
|
for (Map<String, Object> param : lists) {
|
|
@@ -91,11 +110,26 @@ public abstract class BatchDaoService {
|
|
|
sqlSession.clearCache();
|
|
|
}
|
|
|
}
|
|
|
+ success = true;
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ dbExceptionHandler(mapperName, e);
|
|
|
+ throw e;
|
|
|
}
|
|
|
finally {
|
|
|
if (sqlSession != null) {
|
|
|
- sqlSession.commit();
|
|
|
- sqlSession.close();
|
|
|
+ try {
|
|
|
+ if (success) {
|
|
|
+ sqlSession.commit();
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ log.error("[BATCH--DAO] {}:updateBatch: {}: Roll back the transaction due to an error.", this.serviceName, mapperName);
|
|
|
+ sqlSession.rollback();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ sqlSession.close();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
this.count = total;
|