|
|
@@ -30,11 +30,12 @@ import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
|
|
|
import com.jfinal.plugin.activerecord.sql.SqlKit;
|
|
|
|
|
|
public class Config {
|
|
|
+
|
|
|
private final ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
|
|
|
-
|
|
|
+
|
|
|
String name;
|
|
|
DataSource dataSource;
|
|
|
-
|
|
|
+
|
|
|
Dialect dialect;
|
|
|
boolean showSql;
|
|
|
boolean devMode;
|
|
|
@@ -42,14 +43,14 @@ public class Config {
|
|
|
IContainerFactory containerFactory;
|
|
|
IDbProFactory dbProFactory = IDbProFactory.defaultDbProFactory;
|
|
|
ICache cache;
|
|
|
-
|
|
|
+
|
|
|
SqlKit sqlKit;
|
|
|
-
|
|
|
+
|
|
|
// For ActiveRecordPlugin only, dataSource can be null
|
|
|
public Config(String name, DataSource dataSource, int transactionLevel) {
|
|
|
init(name, dataSource, new MysqlDialect(), false, false, transactionLevel, IContainerFactory.defaultContainerFactory, new EhCache());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Constructor with full parameters
|
|
|
* @param name the name of the config
|
|
|
@@ -67,7 +68,7 @@ public class Config {
|
|
|
}
|
|
|
init(name, dataSource, dialect, showSql, devMode, transactionLevel, containerFactory, cache);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private void init(String name, DataSource dataSource, Dialect dialect, boolean showSql, boolean devMode, int transactionLevel, IContainerFactory containerFactory, ICache cache) {
|
|
|
if (StrKit.isBlank(name)) {
|
|
|
throw new IllegalArgumentException("Config name can not be blank");
|
|
|
@@ -81,7 +82,7 @@ public class Config {
|
|
|
if (cache == null) {
|
|
|
throw new IllegalArgumentException("Cache can not be null");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
this.name = name.trim();
|
|
|
this.dataSource = dataSource;
|
|
|
this.dialect = dialect;
|
|
|
@@ -91,33 +92,33 @@ public class Config {
|
|
|
this.setTransactionLevel(transactionLevel);
|
|
|
this.containerFactory = containerFactory;
|
|
|
this.cache = cache;
|
|
|
-
|
|
|
+
|
|
|
this.sqlKit = new SqlKit(this.name, this.devMode);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Constructor with name and dataSource
|
|
|
*/
|
|
|
public Config(String name, DataSource dataSource) {
|
|
|
this(name, dataSource, new MysqlDialect());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Constructor with name, dataSource and dialect
|
|
|
*/
|
|
|
public Config(String name, DataSource dataSource, Dialect dialect) {
|
|
|
this(name, dataSource, dialect, false, false, DbKit.DEFAULT_TRANSACTION_LEVEL, IContainerFactory.defaultContainerFactory, new EhCache());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
private Config() {
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
void setDevMode(boolean devMode) {
|
|
|
this.devMode = devMode;
|
|
|
this.sqlKit.setDevMode(devMode);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
void setTransactionLevel(int transactionLevel) {
|
|
|
int t = transactionLevel;
|
|
|
if (t != 0 && t != 1 && t != 2 && t != 4 && t != 8) {
|
|
|
@@ -125,7 +126,7 @@ public class Config {
|
|
|
}
|
|
|
this.transactionLevel = transactionLevel;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Create broken config for DbKit.brokenConfig = Config.createBrokenConfig();
|
|
|
*/
|
|
|
@@ -139,60 +140,60 @@ public class Config {
|
|
|
ret.cache = new EhCache();
|
|
|
return ret;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public String getName() {
|
|
|
return name;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public SqlKit getSqlKit() {
|
|
|
return sqlKit;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public Dialect getDialect() {
|
|
|
return dialect;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public ICache getCache() {
|
|
|
return cache;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public int getTransactionLevel() {
|
|
|
return transactionLevel;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public DataSource getDataSource() {
|
|
|
return dataSource;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public IContainerFactory getContainerFactory() {
|
|
|
return containerFactory;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public IDbProFactory getDbProFactory() {
|
|
|
return dbProFactory;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public boolean isShowSql() {
|
|
|
return showSql;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public boolean isDevMode() {
|
|
|
return devMode;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// --------
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Support transaction with Transaction interceptor
|
|
|
*/
|
|
|
public void setThreadLocalConnection(Connection connection) {
|
|
|
threadLocal.set(connection);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void removeThreadLocalConnection() {
|
|
|
threadLocal.remove();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Get Connection. Support transaction if Connection in ThreadLocal
|
|
|
*/
|
|
|
@@ -202,7 +203,7 @@ public class Config {
|
|
|
return conn;
|
|
|
return showSql ? new SqlReporter(dataSource.getConnection()).getConnection() : dataSource.getConnection();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Helps to implement nested transaction.
|
|
|
* Tx.intercept(...) and Db.tx(...) need this method to detected if it in nested transaction.
|
|
|
@@ -210,14 +211,14 @@ public class Config {
|
|
|
public Connection getThreadLocalConnection() {
|
|
|
return threadLocal.get();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Return true if current thread in transaction.
|
|
|
*/
|
|
|
public boolean isInTransaction() {
|
|
|
return threadLocal.get() != null;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* Close ResultSet、Statement、Connection
|
|
|
* ThreadLocal support declare transaction.
|
|
|
@@ -225,22 +226,22 @@ public class Config {
|
|
|
public void close(ResultSet rs, Statement st, Connection conn) {
|
|
|
if (rs != null) {try {rs.close();} catch (SQLException e) {LogKit.error(e.getMessage(), e);}}
|
|
|
if (st != null) {try {st.close();} catch (SQLException e) {LogKit.error(e.getMessage(), e);}}
|
|
|
-
|
|
|
+
|
|
|
if (threadLocal.get() == null) { // in transaction if conn in threadlocal
|
|
|
if (conn != null) {try {conn.close();}
|
|
|
catch (SQLException e) {throw new ActiveRecordException(e);}}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void close(Statement st, Connection conn) {
|
|
|
if (st != null) {try {st.close();} catch (SQLException e) {LogKit.error(e.getMessage(), e);}}
|
|
|
-
|
|
|
+
|
|
|
if (threadLocal.get() == null) { // in transaction if conn in threadlocal
|
|
|
if (conn != null) {try {conn.close();}
|
|
|
catch (SQLException e) {throw new ActiveRecordException(e);}}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void close(Connection conn) {
|
|
|
if (threadLocal.get() == null) // in transaction if conn in threadlocal
|
|
|
if (conn != null)
|