use of org.axe.interface_.persistence.BaseDataSource in project Axe by DongyuCai.
the class HomeController method dataSource.
@Request(value = "/dataSource", method = RequestMethod.GET)
public void dataSource(@RequestParam("token") String token, HttpServletRequest request, HttpServletResponse response) {
String contextPath = request.getContextPath();
StringBuilder html = new StringBuilder();
html.append("<!DOCTYPE html>");
html.append("<html>");
html.append("<head>");
html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
html.append("<title>axe datasource</title>");
html.append("</head>");
html.append("<body>");
html.append("<table width=\"100%\">");
html.append("<tr><td align=\"right\">");
if (ConfigHelper.getAxeSignIn()) {
html.append("<a style=\"font-size: 15px;color: #AE0000\" href=\"" + contextPath + "/axe/sign-out?token=" + token + "\"><b>退出</b></a>");
}
html.append(" <a style=\"font-size: 15px;color: #AE0000\" href=\"" + contextPath + "/axe?token=" + token + "\"><b>首页</b></a>");
html.append("</td></tr>");
Map<String, BaseDataSource> dataSourceMap = DataSourceHelper.getDataSourceAll();
html.append("<tr><td align=\"center\"><font size=\"28\">DataSource x" + dataSourceMap.size() + "</font></td></tr>");
html.append("");
html.append("<tr><td><table cellspacing=\"0px\"><tr><td style=\"background-color: #AE0000\">");
html.append(" <font color=\"white\"><b>DataSource</b></font> ");
html.append("</td></tr></table></td></tr>");
html.append("");
html.append("<tr><td height=\"2px\" style=\"background-color: #AE0000\"></td></tr>");
html.append("<tr><td>");
html.append("<table width=\"100%\">");
html.append("<tr style=\"background-color: #F0F0F0;\">");
html.append("<td align=\"left\"> </td>");
html.append("<td align=\"left\"><b>Name</b></td>");
html.append("<td align=\"left\"><b>Driver</b></td>");
html.append("<td align=\"left\"><b>Url</b></td>");
html.append("<td align=\"left\"><b>Username</b></td>");
html.append("<td align=\"left\"><b>Password</b></td>");
html.append("<td align=\"left\"><b>Class</b></td>");
html.append("</tr>");
for (Map.Entry<String, BaseDataSource> entry : dataSourceMap.entrySet()) {
String dataSourceName = entry.getKey();
BaseDataSource dataSource = entry.getValue();
html.append("<tr>");
html.append("<td align=\"left\"> </td>");
html.append("<td align=\"left\">" + dataSourceName + "</td>");
html.append("<td align=\"left\">" + dataSource.setJdbcDriver() + "</td>");
html.append("<td align=\"left\">" + dataSource.setJdbcUrl() + "</td>");
html.append("<td align=\"left\">" + dataSource.setJdbcUserName() + "</td>");
html.append("<td align=\"left\">" + dataSource.setJdbcPassword() + "</td>");
html.append("<td align=\"left\">" + dataSource.getClass() + "</td>");
html.append("</tr>");
}
html.append("</table>");
html.append("</td></tr>");
html.append("</table>");
html.append("</body>");
html.append("</html>");
printHtml(response, html.toString());
}
use of org.axe.interface_.persistence.BaseDataSource in project Axe by DongyuCai.
the class DataBaseHelper method getConnection.
/**
* 获取数据库并链接
* @throws SQLException
*/
public static Connection getConnection(String dataSourceName) throws SQLException {
HashMap<String, Connection> connMap = CONNECTION_HOLDER.get();
if (connMap == null || !connMap.containsKey(dataSourceName)) {
try {
Map<String, BaseDataSource> dsMap = DataSourceHelper.getDataSourceAll();
if (dsMap.containsKey(dataSourceName)) {
Connection connection = dsMap.get(dataSourceName).getConnection();
if (connMap == null) {
connMap = new HashMap<>();
CONNECTION_HOLDER.set(connMap);
}
connMap.put(dataSourceName, connection);
}
} catch (SQLException e) {
LOGGER.error("get connection failure", e);
throw new SQLException(e);
}
}
if (connMap != null && connMap.containsKey(dataSourceName)) {
return connMap.get(dataSourceName);
} else {
throw new RuntimeException("connot find connection of dataSource:" + dataSourceName);
}
}
use of org.axe.interface_.persistence.BaseDataSource in project Axe by DongyuCai.
the class DataBaseHelper method commitTransaction.
/**
* 提交事务
* @throws SQLException
*/
public static void commitTransaction() throws SQLException {
HashMap<String, Connection> connMap = CONNECTION_HOLDER.get();
Map<String, BaseDataSource> dsMap = DataSourceHelper.getDataSourceAll();
if (connMap != null && connMap.size() > 0) {
String errorDataSourceName = null;
try {
for (String dataSourceName : connMap.keySet()) {
if (dsMap.get(dataSourceName).tns()) {
errorDataSourceName = dataSourceName;
Connection conn = connMap.get(dataSourceName);
if (!conn.getAutoCommit()) {
conn.commit();
}
}
}
} catch (SQLException e) {
LOGGER.error("commit transaction of dataSource[" + errorDataSourceName + "] failure", e);
throw new SQLException(e);
} finally {
for (String dataSourceName : connMap.keySet()) {
if (dsMap.get(dataSourceName).tns()) {
if (!connMap.get(dataSourceName).getAutoCommit()) {
closeConnection(dataSourceName);
}
}
}
}
}
}
use of org.axe.interface_.persistence.BaseDataSource in project Axe by DongyuCai.
the class DataBaseHelper method rollbackTransaction.
/**
* 回滚事务
*/
public static void rollbackTransaction() {
HashMap<String, Connection> connMap = CONNECTION_HOLDER.get();
Map<String, BaseDataSource> dsMap = DataSourceHelper.getDataSourceAll();
if (connMap != null && connMap.size() > 0) {
String errorDataSourceName = null;
try {
for (String dataSourceName : connMap.keySet()) {
if (dsMap.get(dataSourceName).tns()) {
errorDataSourceName = dataSourceName;
Connection conn = connMap.get(dataSourceName);
conn.rollback();
}
}
} catch (SQLException e) {
LOGGER.error("rollback transaction of dataSource[" + errorDataSourceName + "] failure", e);
throw new RuntimeException(e);
} finally {
try {
for (String dataSourceName : connMap.keySet()) {
if (dsMap.get(dataSourceName).tns()) {
if (!connMap.get(dataSourceName).getAutoCommit()) {
closeConnection(dataSourceName);
}
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
use of org.axe.interface_.persistence.BaseDataSource in project Axe by DongyuCai.
the class DataSourceHelper method init.
@Override
public void init() {
synchronized (this) {
Set<Class<?>> dataSourceFactoryClassSet = ClassHelper.getClassSetBySuper(BaseDataSource.class);
Map<String, Class<?>> dataSourceFactoryClassMap = new HashMap<>();
for (Class<?> dataSourceFactoryClass : dataSourceFactoryClassSet) {
if (dataSourceFactoryClass.isAnnotationPresent(DataSource.class)) {
String dataSourceName = dataSourceFactoryClass.getAnnotation(DataSource.class).value();
if (StringUtil.isEmpty(dataSourceName))
throw new RuntimeException("find the empty name DataSource:" + dataSourceFactoryClass);
if (dataSourceFactoryClassMap.containsKey(dataSourceName))
throw new RuntimeException("find the same name DataSource:" + DATA_SOURCE.get(dataSourceName).getClass() + "===" + dataSourceFactoryClass);
dataSourceFactoryClassMap.put(dataSourceName, dataSourceFactoryClass);
}
}
DATA_SOURCE = new HashMap<>();
String jdbcDatasource = ConfigHelper.getJdbcDatasource();
String[] split = jdbcDatasource.split(",");
for (String dataSourceNameConfig : split) {
//默认数据源取得是配置数据源列表中的第一个
if (StringUtil.isNotEmpty(dataSourceNameConfig)) {
if (DEFAULT_DATASOURCE_NAME == null) {
DEFAULT_DATASOURCE_NAME = dataSourceNameConfig;
}
if (dataSourceFactoryClassMap.containsKey(dataSourceNameConfig)) {
BaseDataSource dataSource = ReflectionUtil.newInstance(dataSourceFactoryClassMap.get(dataSourceNameConfig));
DATA_SOURCE.put(dataSourceNameConfig, dataSource);
}
}
}
}
}
Aggregations