diff options
| author | chenjinsong <[email protected]> | 2018-11-06 10:56:09 +0800 |
|---|---|---|
| committer | chenjinsong <[email protected]> | 2018-11-06 10:56:09 +0800 |
| commit | 13960035e092793847f43c47ad6e546d706fbbe6 (patch) | |
| tree | e8663f233a1f6625be16d6324e6e297a87acf13e /src | |
| parent | 649dd34def18a5153f8c6a7102956d0abeed1536 (diff) | |
1.增加hibernate会话绑定配置,修复new thread中数据库操作的session过期的问题
2.修复告警信息页面懒加载后显示异常的问题
3.任务列表中不显示非本机任务
Diffstat (limited to 'src')
| -rw-r--r-- | src/conf/spring/applicationContext.xml | 221 | ||||
| -rw-r--r-- | src/nis/nms/persistence/HibernateGeneralDaoImpl.java | 14 | ||||
| -rw-r--r-- | src/nis/nms/util/BaseAction.java | 350 | ||||
| -rw-r--r-- | src/nis/nms/util/Constant.java | 2 | ||||
| -rw-r--r-- | src/nis/nms/util/HibernateUtil.java | 40 |
5 files changed, 466 insertions, 161 deletions
diff --git a/src/conf/spring/applicationContext.xml b/src/conf/spring/applicationContext.xml index d4a648a..0487c39 100644 --- a/src/conf/spring/applicationContext.xml +++ b/src/conf/spring/applicationContext.xml @@ -1,110 +1,111 @@ -<?xml version="1.0" encoding="UTF-8"?> -<beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:aop="http://www.springframework.org/schema/aop" - xmlns:tx="http://www.springframework.org/schema/tx" - xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd - http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-2.5.xsd - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd - http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd" - default-autowire="byName" default-lazy-init="true"> -<!-- - <bean id="sessionFactory" - class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> - </bean> - --> - <bean id="propertyConfigurer" - class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> - <property name="locations"> - <list> - <value>WEB-INF/classes/hibernate/jdbc.properties</value> - </list> - </property> - </bean> - - <bean id="myDataSource" - class="com.mchange.v2.c3p0.ComboPooledDataSource" - destroy-method="close" dependency-check="none"> - <property name="driverClass"> - <value>${jdbc.driver}</value> - </property> - <property name="jdbcUrl"> - <value>${jdbc.url}</value> - </property> - <property name="user"> - <value>${jdbc.username}</value> - </property> - <property name="password"> - <value>${jdbc.password}</value> - </property> - <property name="acquireIncrement"> - <value>${c3p0.acquireIncrement}</value> - </property> - <property name="initialPoolSize"> - <value>${c3p0.initialPoolSize}</value> - </property> - <property name="minPoolSize"> - <value>${c3p0.minPoolSize}</value> - </property> - <property name="maxPoolSize"> - <value>${c3p0.maxPoolSize}</value> - </property> - <property name="maxIdleTime"> - <value>${c3p0.maxIdleTime}</value> - </property> - <property name="idleConnectionTestPeriod"> - <value>${c3p0.idleConnectionTestPeriod}</value> - </property> - <property name="maxStatements"> - <value>${c3p0.maxStatements}</value> - </property> - <property name="numHelperThreads"> - <value>${c3p0.numHelperThreads}</value> - </property> - </bean> - - - <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> - <property name="dataSource" ref="myDataSource" /> - <property name="mappingDirectoryLocations"> - <list> - <value>/WEB-INF/classes/nis/nms/domains/</value> - </list> - </property> - <property name="hibernateProperties"> - <props> - <prop key="hibernate.dialect">${hibernate.dialect}</prop> - <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> - <prop key="hibernate.format_sql">${hibernate.format_sql}</prop> - <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop> - <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop> - </props> - </property> - </bean> - - - <!-- <bean id="transactionManager" - class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> - <property name="dataSource" ref="myDataSource" /> - </bean> --> - - - - - <!-- <aop:config proxy-target-class="true"> - <aop:advisor pointcut="execution(* nis.nms.service..*Service.*(..))" advice-ref="txAdvice" /> - <aop:advisor pointcut="execution(* nis.nms.persistence.*Dao.*(..))" advice-ref="txAdvice"/> - </aop:config> --> - - - - <!-- 定义transactionManager作事务管理,get*方法事务为readonly,其它方法按默认设置 --> - <!-- <tx:advice id="txAdvice"> - <tx:attributes> - <tx:method name="get*" read-only="true" /> - <tx:method name="find*" read-only="true" /> - <tx:method name="*" /> - </tx:attributes> - </tx:advice> --> - -</beans> +<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:aop="http://www.springframework.org/schema/aop"
+ xmlns:tx="http://www.springframework.org/schema/tx"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+ http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-2.5.xsd
+ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
+ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
+ default-autowire="byName" default-lazy-init="true">
+<!--
+ <bean id="sessionFactory"
+ class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
+ </bean>
+ -->
+ <bean id="propertyConfigurer"
+ class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
+ <property name="locations">
+ <list>
+ <value>WEB-INF/classes/hibernate/jdbc.properties</value>
+ </list>
+ </property>
+ </bean>
+
+ <bean id="myDataSource"
+ class="com.mchange.v2.c3p0.ComboPooledDataSource"
+ destroy-method="close" dependency-check="none">
+ <property name="driverClass">
+ <value>${jdbc.driver}</value>
+ </property>
+ <property name="jdbcUrl">
+ <value>${jdbc.url}</value>
+ </property>
+ <property name="user">
+ <value>${jdbc.username}</value>
+ </property>
+ <property name="password">
+ <value>${jdbc.password}</value>
+ </property>
+ <property name="acquireIncrement">
+ <value>${c3p0.acquireIncrement}</value>
+ </property>
+ <property name="initialPoolSize">
+ <value>${c3p0.initialPoolSize}</value>
+ </property>
+ <property name="minPoolSize">
+ <value>${c3p0.minPoolSize}</value>
+ </property>
+ <property name="maxPoolSize">
+ <value>${c3p0.maxPoolSize}</value>
+ </property>
+ <property name="maxIdleTime">
+ <value>${c3p0.maxIdleTime}</value>
+ </property>
+ <property name="idleConnectionTestPeriod">
+ <value>${c3p0.idleConnectionTestPeriod}</value>
+ </property>
+ <property name="maxStatements">
+ <value>${c3p0.maxStatements}</value>
+ </property>
+ <property name="numHelperThreads">
+ <value>${c3p0.numHelperThreads}</value>
+ </property>
+ </bean>
+
+
+ <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
+ <property name="dataSource" ref="myDataSource" />
+ <property name="mappingDirectoryLocations">
+ <list>
+ <value>/WEB-INF/classes/nis/nms/domains/</value>
+ </list>
+ </property>
+ <property name="hibernateProperties">
+ <props>
+ <prop key="hibernate.dialect">${hibernate.dialect}</prop>
+ <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
+ <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
+ <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
+ <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
+ <prop key="hibernate.current_session_context_class">thread</prop>
+ </props>
+ </property>
+ </bean>
+
+
+ <!-- <bean id="transactionManager"
+ class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
+ <property name="dataSource" ref="myDataSource" />
+ </bean> -->
+
+
+
+
+ <!-- <aop:config proxy-target-class="true">
+ <aop:advisor pointcut="execution(* nis.nms.service..*Service.*(..))" advice-ref="txAdvice" />
+ <aop:advisor pointcut="execution(* nis.nms.persistence.*Dao.*(..))" advice-ref="txAdvice"/>
+ </aop:config> -->
+
+
+
+ <!-- 定义transactionManager作事务管理,get*方法事务为readonly,其它方法按默认设置 -->
+ <!-- <tx:advice id="txAdvice">
+ <tx:attributes>
+ <tx:method name="get*" read-only="true" />
+ <tx:method name="find*" read-only="true" />
+ <tx:method name="*" />
+ </tx:attributes>
+ </tx:advice> -->
+
+</beans>
diff --git a/src/nis/nms/persistence/HibernateGeneralDaoImpl.java b/src/nis/nms/persistence/HibernateGeneralDaoImpl.java index 170f161..23549b5 100644 --- a/src/nis/nms/persistence/HibernateGeneralDaoImpl.java +++ b/src/nis/nms/persistence/HibernateGeneralDaoImpl.java @@ -20,6 +20,7 @@ import org.hibernate.Criteria; import org.hibernate.Hibernate; import org.hibernate.Query; import org.hibernate.Session; +import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.criterion.CriteriaSpecification; import org.hibernate.criterion.Criterion; @@ -36,6 +37,7 @@ import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; +import nis.nms.service.CommonService; import nis.nms.util.BaseAction; import nis.nms.util.BeanUtils; import nis.nms.util.GenericUtils; @@ -127,6 +129,12 @@ public class HibernateGeneralDaoImpl<T> extends HibernateDaoSupport implements H } } + public void save(Object entity, SessionFactory sessionFactory) throws Exception { + CommonService<T> c = new CommonService<T>(); + c.setSessionFactory(sessionFactory); + c.save(entity); + } + @Override public void save(Object entity) throws Exception { if(txFlag){ @@ -178,6 +186,12 @@ public class HibernateGeneralDaoImpl<T> extends HibernateDaoSupport implements H } } + public void update(Object entity, SessionFactory sessionFactory) throws Exception { + CommonService<T> c = new CommonService<T>(); + c.setSessionFactory(sessionFactory); + update(entity); + } + @Override public void update(Object entity) throws Exception { if(txFlag){ diff --git a/src/nis/nms/util/BaseAction.java b/src/nis/nms/util/BaseAction.java index 009c42a..31ddbe5 100644 --- a/src/nis/nms/util/BaseAction.java +++ b/src/nis/nms/util/BaseAction.java @@ -53,6 +53,13 @@ import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.struts2.ServletActionContext; +import org.hibernate.FlushMode; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; +import org.springframework.orm.hibernate3.SessionFactoryUtils; +import org.springframework.orm.hibernate3.SessionHolder; +import org.springframework.transaction.support.TransactionSynchronizationManager; import com.nms.thread.socket.SocketClientServeice; import com.opensymphony.xwork2.ActionSupport; @@ -1109,10 +1116,35 @@ public abstract class BaseAction extends ActionSupport implements Serializable @Override public void run() { - - try { - new SocketClientServeice(st.getServerIp()).sendInfoToServer(WEB_NOTICE_SET_INFO_ALERT,jObject.toString()); - } catch (Exception e) { + if (Constant.DC_NOTICE) { + try { + new SocketClientServeice(st.getServerIp()).sendInfoToServer(WEB_NOTICE_SET_INFO_ALERT,jObject.toString()); + } catch (Exception e) { + e.printStackTrace(); + + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + + EventRecordLibrary library = new EventRecordLibrary(); + library.setRecordCommand(WEB_NOTICE_SET_INFO_ALERT); + library.setRecordContent(jObject.toString()); + library.setRecordType("W2S"); + library.setState(1l); + library.setNmsserverId(st.getId()); + library.setCreateTime(new Date()); + try { + commonService.save(library, sessionFactory); + } catch (Exception e1) { + e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); + } + logger.error("Monitoring setting to change communication anomalies: " + e.getMessage()); + } + } else { + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + EventRecordLibrary library = new EventRecordLibrary(); library.setRecordCommand(WEB_NOTICE_SET_INFO_ALERT); library.setRecordContent(jObject.toString()); @@ -1121,11 +1153,12 @@ public abstract class BaseAction extends ActionSupport implements Serializable library.setNmsserverId(st.getId()); library.setCreateTime(new Date()); try { - commonService.save(library); + commonService.save(library, sessionFactory); } catch (Exception e1) { e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); } - logger.error("Monitoring setting to change communication anomalies: " + e.getMessage()); } } @@ -1237,12 +1270,41 @@ public abstract class BaseAction extends ActionSupport implements Serializable new Thread(new Runnable() { @Override public void run() { - try { - new SocketClientServeice(st.getServerIp()). - sendFilesToServer(SEND_PLUGIN_SCRIPT_FILE, pluginFiles); - } catch (Exception e) { - logger.error("Script send failure, " + e.getMessage()); - + if (Constant.DC_NOTICE) { + try { + new SocketClientServeice(st.getServerIp()). + sendFilesToServer(SEND_PLUGIN_SCRIPT_FILE, pluginFiles); + } catch (Exception e) { + logger.error("Script send failure, " + e.getMessage()); + + Map<String, String> recordContent = new HashMap<String, String>(); + HashSet<String> pluginFileNames = new HashSet<String>(); + for (File pluginFile : pluginFiles) { + pluginFileNames.add(pluginFile.getName()); + } + String content = StringUtils.join(pluginFileNames.iterator(), ","); + recordContent.put("scriptNames", content); + + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + + EventRecordLibrary library = new EventRecordLibrary(); + library.setRecordType("W2S"); + library.setRecordCommand(SEND_PLUGIN_SCRIPT_FILE); + library.setRecordContent(JSONObject.fromObject(recordContent).toString()); + library.setState(1L); + library.setNmsserverId(st.getId()); + library.setCreateTime(new Date()); + try { + commonService.save(library, sessionFactory); + } catch (Exception e1) { + e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); + } + + } + } else { Map<String, String> recordContent = new HashMap<String, String>(); HashSet<String> pluginFileNames = new HashSet<String>(); for (File pluginFile : pluginFiles) { @@ -1250,6 +1312,10 @@ public abstract class BaseAction extends ActionSupport implements Serializable } String content = StringUtils.join(pluginFileNames.iterator(), ","); recordContent.put("scriptNames", content); + + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + EventRecordLibrary library = new EventRecordLibrary(); library.setRecordType("W2S"); library.setRecordCommand(SEND_PLUGIN_SCRIPT_FILE); @@ -1258,11 +1324,12 @@ public abstract class BaseAction extends ActionSupport implements Serializable library.setNmsserverId(st.getId()); library.setCreateTime(new Date()); try { - commonService.save(library); + commonService.save(library, sessionFactory); } catch (Exception e1) { e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); } - } } }).start(); @@ -1290,7 +1357,7 @@ public abstract class BaseAction extends ActionSupport implements Serializable + " on ('0,' || dsi.node_groups_id || ',0' like '%,' || ngt.group_id || ',%')" + " left join node_table nt" + " on ('0,' || dsi.node_ips_id || ',0' like '%,' || nt.node_id || ',%')" - + " where and dsi.detection_set_state = 1 and dsi.is_control_start = 2" + + " where dsi.detection_set_state = 1 and dsi.is_control_start = 2" + ")t" + " where 1<>1"; if(nodeId!=null) { @@ -1298,12 +1365,12 @@ public abstract class BaseAction extends ActionSupport implements Serializable } //@2018-3-9 fang 修改 首先查询符合条件的id,在拼接sql,适配mysql String groupIdSql = "select group_id from nodegroup_table t where 1=1 "; - String ids = this.commonService.getGroupIdStartWith(groupIdSql, "t.group_id = " +nodeGroupId); + String ids = commonService.getGroupIdStartWith(groupIdSql, "t.group_id = " +nodeGroupId); /*sql += " or(t.node_ips_id is null and t.node_group_id in(select group_id" + " from nodegroup_table t" + " start with t.group_id = " +nodeGroupId+ " connect by prior t.group_id = parent_group_id))"*/ sql += " or ( t.node_ips_id is null and t.node_group_id in("+ids+")" - + " or(t.node_ips_id is null and t.node_groups_id is null)" + + " or(t.node_ips_id is null and t.node_groups_id is null))" ; List<?> SetInfoIdList = commonService.executeSQL(sql); @@ -1322,12 +1389,41 @@ public abstract class BaseAction extends ActionSupport implements Serializable new Thread(new Runnable() { @Override public void run() { - try { - new SocketClientServeice(st.getServerIp()). - sendFilesToServer(SEND_PLUGIN_SCRIPT_FILE, pluginFiles); - } catch (Exception e) { - logger.error("Script send failure, " + e.getMessage()); - + if (Constant.DC_NOTICE) { + try { + new SocketClientServeice(st.getServerIp()). + sendFilesToServer(SEND_PLUGIN_SCRIPT_FILE, pluginFiles); + } catch (Exception e) { + logger.error("Script send failure, " + e.getMessage()); + + Map<String, String> recordContent = new HashMap<String, String>(); + HashSet<String> pluginFileNames = new HashSet<String>(); + for (File pluginFile : pluginFiles) { + pluginFileNames.add(pluginFile.getName()); + } + String content = StringUtils.join(pluginFileNames.iterator(), ","); + recordContent.put("scriptNames", content); + + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + + EventRecordLibrary library = new EventRecordLibrary(); + library.setRecordType("W2S"); + library.setRecordCommand(SEND_PLUGIN_SCRIPT_FILE); + library.setRecordContent(JSONArray.fromObject(recordContent).toString()); + library.setState(1L); + library.setNmsserverId(st.getId()); + library.setCreateTime(new Date()); + try { + commonService.save(library, sessionFactory); + } catch (Exception e1) { + e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); + } + + } + } else { Map<String, String> recordContent = new HashMap<String, String>(); HashSet<String> pluginFileNames = new HashSet<String>(); for (File pluginFile : pluginFiles) { @@ -1336,6 +1432,9 @@ public abstract class BaseAction extends ActionSupport implements Serializable String content = StringUtils.join(pluginFileNames.iterator(), ","); recordContent.put("scriptNames", content); + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + EventRecordLibrary library = new EventRecordLibrary(); library.setRecordType("W2S"); library.setRecordCommand(SEND_PLUGIN_SCRIPT_FILE); @@ -1344,11 +1443,12 @@ public abstract class BaseAction extends ActionSupport implements Serializable library.setNmsserverId(st.getId()); library.setCreateTime(new Date()); try { - commonService.save(library); + commonService.save(library, sessionFactory); } catch (Exception e1) { e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); } - } } }).start(); @@ -1390,10 +1490,35 @@ public abstract class BaseAction extends ActionSupport implements Serializable @Override public void run() { - try { - new SocketClientServeice(st.getServerIp()).sendInfoToServer(WEB_NOTICE_NODE_AND_GROUP_STATE_ALERT,str); - } catch (Exception e) { - e.printStackTrace(); + if (Constant.DC_NOTICE) { + try { + new SocketClientServeice(st.getServerIp()).sendInfoToServer(WEB_NOTICE_NODE_AND_GROUP_STATE_ALERT,str); + } catch (Exception e) { + e.printStackTrace(); + + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + + EventRecordLibrary library = new EventRecordLibrary(); + library.setRecordCommand(WEB_NOTICE_NODE_AND_GROUP_STATE_ALERT); + library.setRecordContent(str); + library.setRecordType("W2S"); + library.setState(1l); + library.setNmsserverId(st.getId()); + library.setCreateTime(new Date()); + + try { + commonService.save(library, sessionFactory); + } catch (Exception e1) { + e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); + } + } + } else { + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + EventRecordLibrary library = new EventRecordLibrary(); library.setRecordCommand(WEB_NOTICE_NODE_AND_GROUP_STATE_ALERT); library.setRecordContent(str); @@ -1401,10 +1526,13 @@ public abstract class BaseAction extends ActionSupport implements Serializable library.setState(1l); library.setNmsserverId(st.getId()); library.setCreateTime(new Date()); + try { - commonService.save(library); + commonService.save(library, sessionFactory); } catch (Exception e1) { e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); } } } @@ -1443,10 +1571,48 @@ public abstract class BaseAction extends ActionSupport implements Serializable @Override public void run() { - try { - new SocketClientServeice(st.getServerIp()).sendInfoToServer(WEB_NOTICE_ACTIVE_ALARM_START_ALERT,jObject.toString()); - } catch (Exception e) { - e.printStackTrace(); + if (Constant.DC_NOTICE) { + try { + new SocketClientServeice(st.getServerIp()).sendInfoToServer(WEB_NOTICE_ACTIVE_ALARM_START_ALERT,jObject.toString()); + } catch (Exception e) { + e.printStackTrace(); + + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + + try { + List list = commonService.find("from EventRecordLibrary where recordType='W2S' and recordCommand='"+WEB_NOTICE_ACTIVE_ALARM_START_ALERT+"' and nmsserverId="+st.getId()); + EventRecordLibrary library = new EventRecordLibrary(); + if (list != null && list.size() > 0) { + library = (EventRecordLibrary)list.get(0); + + library.setRecordCommand(WEB_NOTICE_ACTIVE_ALARM_START_ALERT); + library.setRecordContent(jObject.toString()); + library.setRecordType("W2S"); + library.setState(1l); + library.setNmsserverId(st.getId()); + library.setCreateTime(new Date()); + + commonService.update(library, sessionFactory); + }else { + library.setRecordCommand(WEB_NOTICE_ACTIVE_ALARM_START_ALERT); + library.setRecordContent(jObject.toString()); + library.setRecordType("W2S"); + library.setState(1l); + library.setNmsserverId(st.getId()); + library.setCreateTime(new Date()); + commonService.save(library, sessionFactory); + } + + } catch (Exception e1) { + e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); + } + } + } else { + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); try { List list = commonService.find("from EventRecordLibrary where recordType='W2S' and recordCommand='"+WEB_NOTICE_ACTIVE_ALARM_START_ALERT+"' and nmsserverId="+st.getId()); @@ -1461,7 +1627,7 @@ public abstract class BaseAction extends ActionSupport implements Serializable library.setNmsserverId(st.getId()); library.setCreateTime(new Date()); - commonService.update(library); + commonService.update(library, sessionFactory); }else { library.setRecordCommand(WEB_NOTICE_ACTIVE_ALARM_START_ALERT); library.setRecordContent(jObject.toString()); @@ -1469,11 +1635,13 @@ public abstract class BaseAction extends ActionSupport implements Serializable library.setState(1l); library.setNmsserverId(st.getId()); library.setCreateTime(new Date()); - commonService.save(library); + commonService.save(library, sessionFactory); } } catch (Exception e1) { e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); } } } @@ -1502,14 +1670,19 @@ public abstract class BaseAction extends ActionSupport implements Serializable List<ServerTable> stList = getALlNMSServerList(commonService); if(stList!= null && stList.size()>0){ for(final ServerTable st : stList){ - new Thread(new Runnable(){ + new Thread(new Runnable(){ - @Override - public void run() { + @Override + public void run() { + if (Constant.DC_NOTICE) { try { new SocketClientServeice(st.getServerIp()).sendInfoToServer(WEB_NOTICE_CHECK_TYPE_ALERT,checkTypeId); } catch (Exception e) { e.printStackTrace(); + + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + EventRecordLibrary library = new EventRecordLibrary(); library.setRecordCommand(WEB_NOTICE_CHECK_TYPE_ALERT); library.setRecordContent(checkTypeId); @@ -1518,15 +1691,36 @@ public abstract class BaseAction extends ActionSupport implements Serializable library.setCreateTime(new Date()); library.setNmsserverId(st.getId()); try { - commonService.save(library); + commonService.save(library, sessionFactory); } catch (Exception e1) { e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); } } + } else { + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + EventRecordLibrary library = new EventRecordLibrary(); + library.setRecordCommand(WEB_NOTICE_CHECK_TYPE_ALERT); + library.setRecordContent(checkTypeId); + library.setRecordType("W2S"); + library.setState(1l); + library.setCreateTime(new Date()); + library.setNmsserverId(st.getId()); + try { + commonService.save(library, sessionFactory); + } catch (Exception e1) { + e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); + } } - }).start(); + } + + }).start(); } } } catch (Exception e) { @@ -1552,10 +1746,34 @@ public abstract class BaseAction extends ActionSupport implements Serializable new Thread(new Runnable(){ @Override public void run() { - try { - new SocketClientServeice(st.getServerIp()).sendInfoToServer(WEB_NOTICE_MISSION_INFO_ALERT,str); - } catch (Exception e) { - e.printStackTrace(); + if (Constant.DC_NOTICE) { + try { + new SocketClientServeice(st.getServerIp()).sendInfoToServer(WEB_NOTICE_MISSION_INFO_ALERT,str); + } catch (Exception e) { + e.printStackTrace(); + + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + + EventRecordLibrary library = new EventRecordLibrary(); + library.setRecordCommand(WEB_NOTICE_MISSION_INFO_ALERT); + library.setRecordContent(str); + library.setRecordType("W2S"); + library.setState(1l); + library.setCreateTime(new Date()); + library.setNmsserverId(st.getId()); + try { + commonService.save(library, sessionFactory); + } catch (Exception e1) { + e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); + } + } + } else { + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + EventRecordLibrary library = new EventRecordLibrary(); library.setRecordCommand(WEB_NOTICE_MISSION_INFO_ALERT); library.setRecordContent(str); @@ -1564,9 +1782,11 @@ public abstract class BaseAction extends ActionSupport implements Serializable library.setCreateTime(new Date()); library.setNmsserverId(st.getId()); try { - commonService.save(library); + commonService.save(library, sessionFactory); } catch (Exception e1) { e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); } } } @@ -1601,10 +1821,34 @@ public abstract class BaseAction extends ActionSupport implements Serializable @Override public void run() { String content = "1"; - try { - new SocketClientServeice(ip).sendInfoToServer(WEB_NOTICE_DATACONTROLLER,content); - } catch (Exception e) { - e.printStackTrace(); + if (Constant.DC_NOTICE) { + try { + new SocketClientServeice(ip).sendInfoToServer(WEB_NOTICE_DATACONTROLLER,content); + } catch (Exception e) { + e.printStackTrace(); + + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + + EventRecordLibrary library = new EventRecordLibrary(); + library.setRecordCommand(WEB_NOTICE_DATACONTROLLER); + library.setRecordContent(content); + library.setRecordType("W2S"); + library.setState(1l); + library.setCreateTime(new Date()); + library.setNmsserverId(dcId); + try { + commonService.save(library, sessionFactory); + } catch (Exception e1) { + e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); + } + } + } else { + SessionFactory sessionFactory = commonService.getSessionFactory(); + boolean participate = HibernateUtil.bindHibernateSessionToThread(sessionFactory); + EventRecordLibrary library = new EventRecordLibrary(); library.setRecordCommand(WEB_NOTICE_DATACONTROLLER); library.setRecordContent(content); @@ -1613,9 +1857,11 @@ public abstract class BaseAction extends ActionSupport implements Serializable library.setCreateTime(new Date()); library.setNmsserverId(dcId); try { - commonService.save(library); + commonService.save(library, sessionFactory); } catch (Exception e1) { e1.printStackTrace(); + } finally { + HibernateUtil.closeHibernateSessionFromThread(participate, sessionFactory); } } } @@ -1673,4 +1919,6 @@ public abstract class BaseAction extends ActionSupport implements Serializable public void setThisPageUrl(String thisPageUrl) { this.thisPageUrl = thisPageUrl; } + } + diff --git a/src/nis/nms/util/Constant.java b/src/nis/nms/util/Constant.java index 585dfce..1330959 100644 --- a/src/nis/nms/util/Constant.java +++ b/src/nis/nms/util/Constant.java @@ -118,6 +118,8 @@ public class Constant { * */ public static final String EMAIL_FLAG="emailFlag"; + public static final boolean DC_NOTICE = "1".equals(rb.getString("dc.notice")) ? true : false; + /** * 用户部门功能 * */ diff --git a/src/nis/nms/util/HibernateUtil.java b/src/nis/nms/util/HibernateUtil.java new file mode 100644 index 0000000..475a45a --- /dev/null +++ b/src/nis/nms/util/HibernateUtil.java @@ -0,0 +1,40 @@ +package nis.nms.util;
+
+import org.hibernate.FlushMode;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.springframework.orm.hibernate3.SessionFactoryUtils;
+import org.springframework.orm.hibernate3.SessionHolder;
+import org.springframework.transaction.support.TransactionSynchronizationManager;
+
+public class HibernateUtil {
+
+ /**
+ * 将hibernate的会话绑定给一个线程
+ * @param sessionFactory
+ * @return
+ */
+ public static boolean bindHibernateSessionToThread(SessionFactory sessionFactory) {
+ if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
+ return true;
+ } else {
+ Session session = sessionFactory.openSession();
+ session.setFlushMode(FlushMode.MANUAL);
+ SessionHolder sessionHolder = new SessionHolder(session);
+ TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);
+ }
+ return false;
+ }
+
+ /**
+ * 将线程上绑定的hibernate会话关闭
+ * @param participate
+ * @param sessionFactory
+ */
+ public static void closeHibernateSessionFromThread(boolean participate, Object sessionFactory) {
+ if (!participate) {
+ SessionHolder sessionHolder = (SessionHolder)TransactionSynchronizationManager.unbindResource(sessionFactory);
+ SessionFactoryUtils.closeSession(sessionHolder.getSession());
+ }
+ }
+}
|
