<commit_before>package com.haozileung.infra.utils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.pool.DruidDataSource;

public class DataSourceUtil {
	private static final Logger logger = LoggerFactory
			.getLogger(DataSourceUtil.class);
	private final static ThreadLocal<Connection> conns = new ThreadLocal<Connection>();
	private static DruidDataSource druidDataSource;

	private static boolean show_sql = true;

	public final static Connection getConnection() throws SQLException {
		if (null == druidDataSource) {
			druidDataSource = new DruidDataSource();
			druidDataSource.setUrl(PropertiesUtil.getProperties().getProperty(
					"db.url"));
			druidDataSource.setUsername(PropertiesUtil.getProperties()
					.getProperty("db.username"));
			druidDataSource.setPassword(PropertiesUtil.getProperties()
					.getProperty("db.password"));
			druidDataSource.setInitialSize(1);
			druidDataSource.setMinIdle(1);
			druidDataSource.setMaxActive(20);
			druidDataSource.setMaxWait(6000);
			druidDataSource.setTimeBetweenEvictionRunsMillis(6000);
			druidDataSource.setMinEvictableIdleTimeMillis(30000);
			druidDataSource.setValidationQuery("SELECT 'x'");
			druidDataSource.setTestWhileIdle(true);
			druidDataSource.setTestOnBorrow(false);
			druidDataSource.setTestOnReturn(false);
			druidDataSource.setUseGlobalDataSourceStat(true);
			try {
				druidDataSource.setFilters("stat,wall");
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
			}
			try {
				druidDataSource.init();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
			}
			String show_sql_prop = PropertiesUtil.getProperties().getProperty(
					"show_sql");
			show_sql = ((show_sql_prop != null) && show_sql_prop
					.equalsIgnoreCase("true"));
		}
		Connection conn = conns.get();
		if (conn == null || conn.isClosed()) {
			conn = druidDataSource.getConnection();
			conn.setAutoCommit(false);
			conns.set(conn);
		}
		return (show_sql && !Proxy.isProxyClass(conn.getClass())) ? new _DebugConnection(
				conn).getConnection() : conn;
	}

	/**
	 * 关闭连接
	 */
	public final static void closeConnection(boolean rollback) {
		Connection conn = conns.get();
		if (conn != null) {
			if (rollback) {
				try {
					if (!conn.isClosed()) {
						conn.rollback();
						conn.close();
					}
				} catch (SQLException e) {
					logger.error("Unabled to rollback connection!!! ", e);
				}
			} else {
				try {
					if (!conn.isClosed()) {
						conn.commit();
					}<commit_msg>修改關閉鏈接<commit_after>package com.haozileung.infra.utils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.druid.pool.DruidDataSource;

public class DataSourceUtil {
	private static final Logger logger = LoggerFactory
			.getLogger(DataSourceUtil.class);
	private final static ThreadLocal<Connection> conns = new ThreadLocal<Connection>();
	private static DruidDataSource druidDataSource;

	private static boolean show_sql = true;

	public final static Connection getConnection() throws SQLException {
		if (null == druidDataSource) {
			druidDataSource = new DruidDataSource();
			druidDataSource.setUrl(PropertiesUtil.getProperties().getProperty(
					"db.url"));
			druidDataSource.setUsername(PropertiesUtil.getProperties()
					.getProperty("db.username"));
			druidDataSource.setPassword(PropertiesUtil.getProperties()
					.getProperty("db.password"));
			druidDataSource.setInitialSize(1);
			druidDataSource.setMinIdle(1);
			druidDataSource.setMaxActive(20);
			druidDataSource.setMaxWait(6000);
			druidDataSource.setTimeBetweenEvictionRunsMillis(6000);
			druidDataSource.setMinEvictableIdleTimeMillis(30000);
			druidDataSource.setValidationQuery("SELECT 'x'");
			druidDataSource.setTestWhileIdle(true);
			druidDataSource.setTestOnBorrow(false);
			druidDataSource.setTestOnReturn(false);
			druidDataSource.setUseGlobalDataSourceStat(true);
			try {
				druidDataSource.setFilters("stat,wall");
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
			}
			try {
				druidDataSource.init();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
			}
			String show_sql_prop = PropertiesUtil.getProperties().getProperty(
					"show_sql");
			show_sql = ((show_sql_prop != null) && show_sql_prop
					.equalsIgnoreCase("true"));
		}
		Connection conn = conns.get();
		if (conn == null || conn.isClosed()) {
			conn = druidDataSource.getConnection();
			conn.setAutoCommit(false);
			conns.set(conn);
		}
		return (show_sql && !Proxy.isProxyClass(conn.getClass())) ? new _DebugConnection(
				conn).getConnection() : conn;
	}

	/**
	 * 关闭连接
	 */
	public final static void closeConnection(boolean rollback) {
		Connection conn = conns.get();
		if (conn != null) {
			if (rollback) {
				try {
					if (!conn.isClosed()) {
						conn.rollback();
					}
				} catch (SQLException e) {
					logger.error("Unabled to rollback connection!!! ", e);
				} finally {
					try {
						conn.close();
					} catch (SQLException e) {
						logger.error("Unabled to close connection!!! ", e);
					}
				}
			} else {
				try {
					if (!conn.isClosed()) {
						conn.commit();
					}
{'fraction_non_alphanumeric': 0.12475424486148347, 'fraction_numerical': 0.006791778373547811, 'mean_word_length': 4.309297912713473, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5998650', 'n_tokens_mistral': 2059, 'n_tokens_neox': 1764, 'n_words': 312}