com.qa.framework.library.database.DBHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smart-api-framework Show documentation
Show all versions of smart-api-framework Show documentation
Support web service api automaton test based on testng and httpclient
package com.qa.framework.library.database;
import com.qa.framework.config.PropConfig;
import com.qa.framework.library.base.ClassHelper;
import com.qa.framework.library.base.CollectionHelper;
import com.qa.framework.library.base.StringHelper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* The type Db DBHelper.
*/
public class DBHelper {
private final static Logger logger = Logger.getLogger(DBHelper.class);
private static final ThreadLocal connContainer = new ThreadLocal();
private static final QueryRunner queryRunner = new QueryRunner();
private static String poolName;
static {
String dbPoolName = PropConfig.getDbPoolName();
if (dbPoolName == null) {
String webPath = PropConfig.getWebPath();
if (StringHelper.startsWithIgnoreCase(webPath, "http://")) {
if (webPath.contains("/")) {
poolName = StringHelper.getTokensList(webPath.substring(7), "/").get(0);
} else {
poolName = webPath.substring(7);
}
}
} else {
poolName = dbPoolName;
}
}
/**
* 获取数据库连接
*
* @param poolname the poolname
* @return the connection
*/
public static Connection getConnection(String poolname) {
Connection conn = connContainer.get();
if (conn == null) {
try {
conn = DBPoolFactory.getDbConnection(poolname);
} catch (SQLException e) {
logger.error("get connection failure", e);
throw new RuntimeException(e);
} finally {
connContainer.set(conn);
}
}
return conn;
}
/**
* Gets connection.
*
* @return the connection
*/
public static Connection getConnection() {
return getConnection(poolName);
}
/**
* 开启事务
*/
public static void beginTransaction() {
Connection conn = getConnection();
if (conn != null) {
try {
conn.setAutoCommit(false);
} catch (SQLException e) {
logger.error("开启事务出错!", e);
throw new RuntimeException(e);
} finally {
connContainer.set(conn);
}
}
}
/**
* 提交事务
*/
public static void commitTransaction() {
Connection conn = getConnection();
if (conn != null) {
try {
conn.commit();
conn.close();
} catch (SQLException e) {
logger.error("提交事务出错!", e);
throw new RuntimeException(e);
} finally {
connContainer.remove();
}
}
}
/**
* 回滚事务
*/
public static void rollbackTransaction() {
Connection conn = getConnection();
if (conn != null) {
try {
conn.rollback();
conn.close();
} catch (SQLException e) {
logger.error("回滚事务出错!", e);
throw new RuntimeException(e);
} finally {
connContainer.remove();
}
}
}
/**
* 初始化 SQL 脚本
*
* @param sqlPath the sql path
*/
public static void initSQL(String sqlPath) {
try {
File sqlFile = new File(ClassHelper.getClassPath() + sqlPath);
List sqlList = FileUtils.readLines(sqlFile);
for (String sql : sqlList) {
executeUpdate(sql);
}
} catch (Exception e) {
logger.error("初始化 SQL 脚本出错!", e);
throw new RuntimeException(e);
}
}
/**
* 执行查询语句
*
* @param sql the sql
* @param params the params
* @return the list
*/
public static List