com.bixuebihui.jdbc.MSDbHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of c-dbtools Show documentation
Show all versions of c-dbtools Show documentation
a fast small database connection pool and a active record flavor mini framework
package com.bixuebihui.jdbc;
import com.bixuebihui.DbException;
import com.bixuebihui.shardingjdbc.core.constant.SQLType;
import com.bixuebihui.shardingjdbc.core.hint.HintManagerHolder;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
/**
* Master-Slaver mode database helper class.
* Master connection manager for update, insert and delete
* Slaver connection for select only.
*
* @author [email protected]
* @version $Id: $Id
*/
public class MSDbHelper extends DbHelper {
/**
* The DML_FLAG adopted from ShadingJDBC
*/
private static final ThreadLocal DML_FLAG = ThreadLocal.withInitial(() -> false);
/**
* reset DML flag.
*/
public static void resetDMLFlag() {
DML_FLAG.remove();
}
/**
* master database for change
*/
private DataSource masterDatasource = null;
/**
* {@inheritDoc}
*/
@Override
public Connection getConnection(boolean readOnly) {
try {
if (!readOnly) {
DML_FLAG.set(true);
return masterDatasource.getConnection();
} else if (isMasterRoute(SQLType.DQL)) {
return masterDatasource.getConnection();
}
Connection conn = super.getConnection();
conn.setReadOnly(readOnly);
return conn;
}catch (SQLException e){
throw new DbException(e);
}
}
/**
* getConnection.
*
* @param sqlType a {@link SQLType} object.
* @return a {@link java.sql.Connection} object.
* @throws java.sql.SQLException if any.
*/
public Connection getConnection(final SQLType sqlType) throws SQLException {
if (isMasterRoute(sqlType)) {
DML_FLAG.set(true);
return masterDatasource.getConnection();
}
return super.getConnection();
}
private boolean isMasterRoute(final SQLType sqlType) {
return SQLType.DQL != sqlType || DML_FLAG.get() || HintManagerHolder.isMasterRouteOnly();
}
/**
* getConnection.
*
* @return a {@link java.sql.Connection} object.
*/
@Override
public Connection getConnection() {
return getConnection(false);
}
/**
* Getter for the field masterDatasource
.
*
* @return a {@link javax.sql.DataSource} object.
*/
public DataSource getMasterDatasource() {
return masterDatasource;
}
/**
* Setter for the field masterDatasource
.
*
* @param masterDatasource a {@link javax.sql.DataSource} object.
*/
public void setMasterDatasource(DataSource masterDatasource) {
this.masterDatasource = masterDatasource;
}
/** {@inheritDoc} */
@Override
public void close() {
HintManagerHolder.clear();
resetDMLFlag();
super.close();
}
}