Skip to content

Commit

Permalink
Merge pull request MyCATApache#107 from sohudo/master
Browse files Browse the repository at this point in the history
sqlite并发写问题
  • Loading branch information
mycatmerger committed Mar 28, 2016
2 parents 3dfc4b2 + ce9cfce commit d7ecb59
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 241 deletions.
29 changes: 29 additions & 0 deletions src/main/java/org/mycat/web/task/common/SqliteStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.mycat.web.task.common;

import java.util.Map;

import org.hx.rainbow.common.dao.Dao;

public class SqliteStore {
private volatile static SqliteStore sqliteStore = null;
private SqliteStore(){};

public static SqliteStore getInstance(){
if(sqliteStore == null){
synchronized (SqliteStore.class) {
if(sqliteStore == null){
sqliteStore = new SqliteStore();
}
}
}
return sqliteStore;
}

public synchronized void insert(Dao dao, String namespace,String statement, Map<String,Object> paramData){
dao.insert(namespace, statement, paramData);
}

public synchronized void delete(Dao dao, String namespace,String statement, Map<String,Object> paramData){
dao.delete(namespace, statement, paramData);
}
}
13 changes: 8 additions & 5 deletions src/main/java/org/mycat/web/task/server/SyncClearData.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

import org.hx.rainbow.common.context.RainbowProperties;
import org.hx.rainbow.common.core.SpringApplicationContext;
import org.hx.rainbow.common.dao.Dao;
import org.hx.rainbow.common.util.DateUtil;
import org.mycat.web.service.ShowService;
import org.mycat.web.task.common.ITask;
import org.mycat.web.task.common.SqliteStore;

public class SyncClearData implements ITask {
private static final String NAMESPACE_SYSSQL = "SYSSQL";
Expand All @@ -28,15 +30,16 @@ public void excute(String dbName, Date nowDate) {
Map<String,Object> map=new HashMap();
map.put("clearday", DateUtil.toDateTimeString(DateUtil.addDate(DateUtil.currentDate(), -clearday)));
ShowService showService = (ShowService)SpringApplicationContext.getBean("showService");
showService.getDao().delete(NAMESPACE_SYSSQL, "delete", map);
Dao dao = showService.getDao();
SqliteStore.getInstance().delete(dao, NAMESPACE_SYSSQL, "delete", map);
map.clear();
map.put("clearday", DateUtil.toDateTimeString(DateUtil.addDate(DateUtil.currentDate(), -clearday*2)));
showService.getDao().delete(NAMESPACE_SYSSQLTABLE, "delete", map);
SqliteStore.getInstance().delete(dao, NAMESPACE_SYSSQLTABLE, "delete", map);
map.clear();
map.put("clearday", DateUtil.toDateTimeString(DateUtil.addDate(DateUtil.currentDate(), -clearday*3)));
showService.getDao().delete(NAMESPACE_SYSSQLSUM, "delete", map);
showService.getDao().delete(NAMESPACE_SYSSQLHIGH, "delete", map);
showService.getDao().delete(NAMESPACE_SYSSQLSLOW, "delete", map);
SqliteStore.getInstance().delete(dao, NAMESPACE_SYSSQLSUM, "delete", map);
SqliteStore.getInstance().delete(dao, NAMESPACE_SYSSQLHIGH, "delete", map);
SqliteStore.getInstance().delete(dao, NAMESPACE_SYSSQLSLOW, "delete", map);

}

Expand Down
83 changes: 42 additions & 41 deletions src/main/java/org/mycat/web/task/server/SyncSysSql.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
package org.mycat.web.task.server;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hx.rainbow.common.core.SpringApplicationContext;
import org.mycat.web.service.ShowService;
import org.mycat.web.task.common.ITask;
import org.mycat.web.util.DataSourceUtils;
import org.hx.rainbow.common.util.DateUtil;
/*
* 异步持久化mycat中数据
*/

public class SyncSysSql implements ITask {

private static final String NAMESPACE = "SYSSQL";

private static final String SYSPARAM_NAMESPACE = "SYSSHOW";

@Override
public void excute(String dbName, Date nowDate) {
//System.out.println("持久化mycat中数据:"+dbName);
// if (!DataSourceUtils.getInstance().isMycatManger(dbName)){
// return ;
// }
//System.out.println("持久化mycat中数据开始:"+dbName);
ShowService showService = (ShowService)SpringApplicationContext.getBean("showService");
List<Map<String,Object>> list = showService.getDao().query(dbName, SYSPARAM_NAMESPACE, "sql");
for(Map<String,Object> entry : list){
entry.put("START_TM", DateUtil.toDateTimeString(new Date((long) entry.get("START_TIME"))));
entry.put("DB_NAME", DataSourceUtils.getInstance().getDbName(dbName));
//Map<String,Object> entity = showService.getDao().get(NAMESPACE, "query",entry);
//if(entity == null){
showService.getDao().insert(NAMESPACE, "insert", entry);
//}
}
}

}
package org.mycat.web.task.server;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hx.rainbow.common.core.SpringApplicationContext;
import org.mycat.web.service.ShowService;
import org.mycat.web.task.common.ITask;
import org.mycat.web.task.common.SqliteStore;
import org.mycat.web.util.DataSourceUtils;
import org.hx.rainbow.common.util.DateUtil;
/*
* 异步持久化mycat中数据
*/

public class SyncSysSql implements ITask {

private static final String NAMESPACE = "SYSSQL";

private static final String SYSPARAM_NAMESPACE = "SYSSHOW";

@Override
public void excute(String dbName, Date nowDate) {
//System.out.println("持久化mycat中数据:"+dbName);
// if (!DataSourceUtils.getInstance().isMycatManger(dbName)){
// return ;
// }
//System.out.println("持久化mycat中数据开始:"+dbName);
ShowService showService = (ShowService)SpringApplicationContext.getBean("showService");
List<Map<String,Object>> list = showService.getDao().query(dbName, SYSPARAM_NAMESPACE, "sql");
for(Map<String,Object> entry : list){
entry.put("START_TM", DateUtil.toDateTimeString(new Date((long) entry.get("START_TIME"))));
entry.put("DB_NAME", DataSourceUtils.getInstance().getDbName(dbName));
//Map<String,Object> entity = showService.getDao().get(NAMESPACE, "query",entry);
//if(entity == null){
SqliteStore.getInstance().insert(showService.getDao(), NAMESPACE, "insert", entry);
//}
}
}

}
85 changes: 43 additions & 42 deletions src/main/java/org/mycat/web/task/server/SyncSysSqlhigh.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
package org.mycat.web.task.server;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hx.rainbow.common.core.SpringApplicationContext;
import org.hx.rainbow.common.util.DateUtil;
import org.mycat.web.service.ShowService;
import org.mycat.web.task.common.ITask;
import org.mycat.web.util.DataSourceUtils;

/*
* 异步持久化mycat中数据
*/

public class SyncSysSqlhigh implements ITask {

private static final String NAMESPACE = "SYSSQLHIGH";

private static final String SYSPARAM_NAMESPACE = "SYSSHOW";

@Override
public void excute(String dbName, Date nowDate) {
// if (!DataSourceUtils.getInstance().isMycatManger(dbName)){
// return ;
// }
ShowService showService = (ShowService)SpringApplicationContext.getBean("showService");
List<Map<String,Object>> list = showService.getDao().query(dbName, SYSPARAM_NAMESPACE, "sqlhigh");
for(Map<String,Object> entry : list){
entry.put("LAST_TM", DateUtil.toDateTimeString(new Date((long) entry.get("LAST_TIME"))));
entry.put("DB_NAME", DataSourceUtils.getInstance().getDbName(dbName));
//Map<String,Object> entity = showService.getDao().get(NAMESPACE, "query",entry);
//if(entity == null){
showService.getDao().insert(NAMESPACE, "insert", entry);
//}
}


}

}
package org.mycat.web.task.server;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hx.rainbow.common.core.SpringApplicationContext;
import org.hx.rainbow.common.util.DateUtil;
import org.mycat.web.service.ShowService;
import org.mycat.web.task.common.ITask;
import org.mycat.web.task.common.SqliteStore;
import org.mycat.web.util.DataSourceUtils;

/*
* 异步持久化mycat中数据
*/

public class SyncSysSqlhigh implements ITask {

private static final String NAMESPACE = "SYSSQLHIGH";

private static final String SYSPARAM_NAMESPACE = "SYSSHOW";

@Override
public void excute(String dbName, Date nowDate) {
// if (!DataSourceUtils.getInstance().isMycatManger(dbName)){
// return ;
// }
ShowService showService = (ShowService)SpringApplicationContext.getBean("showService");
List<Map<String,Object>> list = showService.getDao().query(dbName, SYSPARAM_NAMESPACE, "sqlhigh");
for(Map<String,Object> entry : list){
entry.put("LAST_TM", DateUtil.toDateTimeString(new Date((long) entry.get("LAST_TIME"))));
entry.put("DB_NAME", DataSourceUtils.getInstance().getDbName(dbName));
//Map<String,Object> entity = showService.getDao().get(NAMESPACE, "query",entry);
//if(entity == null){
SqliteStore.getInstance().insert(showService.getDao(), NAMESPACE, "insert", entry);
//}
}


}

}
94 changes: 44 additions & 50 deletions src/main/java/org/mycat/web/task/server/SyncSysSqlslow.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,44 @@
package org.mycat.web.task.server;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hx.rainbow.common.core.SpringApplicationContext;
import org.hx.rainbow.common.util.DateUtil;
import org.mycat.web.service.ShowService;
import org.mycat.web.task.common.ITask;
import org.mycat.web.util.DataSourceUtils;
import org.mycat.web.util.MailUtil;

/*
* 异步持久化mycat中数据
*/

public class SyncSysSqlslow implements ITask {

private static final String NAMESPACE = "SYSSQLSLOW";

private static final String SYSPARAM_NAMESPACE = "SYSSHOW";

@Override
public void excute(String dbName, Date nowDate) {
// if (!DataSourceUtils.getInstance().isMycatManger(dbName)){
// return ;
// }
ShowService showService = (ShowService)SpringApplicationContext.getBean("showService");
List<Map<String,Object>> list = showService.getDao().query(dbName, SYSPARAM_NAMESPACE, "sqlslow");
for(Map<String,Object> entry : list){
//entry.put("START_TM", new Date((long) entry.get("START_TIME")));
entry.put("START_TM", DateUtil.toDateTimeString(new Date((long) entry.get("START_TIME"))));
entry.put("DB_NAME", DataSourceUtils.getInstance().getDbName(dbName));
//Map<String,Object> entity = showService.getDao().get(NAMESPACE, "query",entry);
//if(entity == null){
showService.getDao().insert(NAMESPACE, "insert", entry);
//}


try {
MailUtil.send("监控到慢SQL", (String)entry.get("SQL"));
} catch (Exception e) {
}
}


}

}
package org.mycat.web.task.server;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hx.rainbow.common.core.SpringApplicationContext;
import org.hx.rainbow.common.util.DateUtil;
import org.mycat.web.service.ShowService;
import org.mycat.web.task.common.ITask;
import org.mycat.web.task.common.SqliteStore;
import org.mycat.web.util.DataSourceUtils;

/*
* 异步持久化mycat中数据
*/

public class SyncSysSqlslow implements ITask {

private static final String NAMESPACE = "SYSSQLSLOW";

private static final String SYSPARAM_NAMESPACE = "SYSSHOW";

@Override
public void excute(String dbName, Date nowDate) {
// if (!DataSourceUtils.getInstance().isMycatManger(dbName)){
// return ;
// }
ShowService showService = (ShowService)SpringApplicationContext.getBean("showService");
List<Map<String,Object>> list = showService.getDao().query(dbName, SYSPARAM_NAMESPACE, "sqlslow");
for(Map<String,Object> entry : list){
//entry.put("START_TM", new Date((long) entry.get("START_TIME")));
entry.put("START_TM", DateUtil.toDateTimeString(new Date((long) entry.get("START_TIME"))));
entry.put("DB_NAME", DataSourceUtils.getInstance().getDbName(dbName));
//Map<String,Object> entity = showService.getDao().get(NAMESPACE, "query",entry);
//if(entity == null){
SqliteStore.getInstance().insert(showService.getDao(), NAMESPACE, "insert", entry);
//}
}


}

}
Loading

0 comments on commit d7ecb59

Please sign in to comment.