数据库连接释放_java_多线程

写了一个关于策略的自动处理模块,用到了java多线程;
其中数据库连接在多处获取,释放很麻烦;
采用一种方法解决了。
{有人踩了,可能是没耐心看完代码吧,那我就叙述一下:
1、把线程方法内用到的链接全部提取出来,用参数传入
2、在线程方法外初始一条链接传入。(参考方法:makeSmS())

}
供自己以后参考。
函数调用代码:

//start
TimerThead o= PStack.timerMap.get(code);
if(o!=null){
o.TimerStop();
PStack.timerMap.remove(code);

}
Send_strategy strategy=StrategyUtil.getDate(code);
if(strategy.getSMS_SEND()==1){
TimerThead tt=new TimerThead(code,StrategyUtil.getYanchi(strategy),strategy.getRATE());
tt.TimerStart();
PStack.timerMap.put(code,tt);
}
//end

简单定时器代码:

package com.towery.sms.strategy;

import java.util.Timer;
import java.util.TimerTask;
public class TimerThead{
//简单定时器
public Timer timer= new Timer();
private String code;
private int yanchi;
private int pinlv;
//构造方法,传递参数
public TimerThead(String code,int yanchi,int pinlv){
this.code=code;
this.yanchi=yanchi;
this.pinlv=pinlv;
}
/***
* 停止 Timer
*/
public void TimerStop(){
//对应线程停止
timer.cancel();
}
/***
* 开始Timer
*/
public void TimerStart() {
timer.schedule(//参数: 任务,延迟,频率;
new TimerTask() { //TimerTask 继承了Runnabel接口。启用了新的线程
public void run() {
SmsThread st=new SmsThread(code);
st.start();
}
}
,yanchi *60 * 1000,pinlv * 60 * 1000);
}

/**
* main
* @param args
*/
public static void main(String[] args) {
// //start
// TimerThead o= PStack.timerMap.get(code);
// if(o!=null){
// o.TimerStop();
// PStack.timerMap.remove(code);
//
// }
// Send_strategy strategy=StrategyUtil.getDate(code);
// if(strategy.getSMS_SEND()==1){
// TimerThead tt=new TimerThead(code,StrategyUtil.getYanchi(strategy),strategy.getRATE());
// tt.TimerStart();
// PStack.timerMap.put(code,tt);
// }
// //end
}
}


线程类代码:

package com.towery.sms.strategy;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import com.towery.DateUtil;
import com.towery.flowinfo.SystemBus;
import com.towery.flowinfo.bean.Send_strategy;
import com.towery.sms.helper.Receiver;

public class SmsThread extends Thread{
private String code;
public SmsThread(String code){
this.code=code;
}

/***
* 线程入口
*/
public void run() {
makeSmS(code);
}

/***
* 发送短信
* @param code
*/
private void makeSmS(String code){
//时区矫正
TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8");
TimeZone.setDefault(tz);
Connection conn = SystemBus.getConnection();
try{
//获取策略数据
Send_strategy strategy=StrategyUtil.getDate(conn,code);
//获取并编辑发送对象及内容
Map contentMap= getSms(conn,code, strategy);
//解析并发送
doWrite(conn,code,contentMap);

}catch(Exception e){
e.printStackTrace();
}finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn=null;
}
}
}
/***
* 获取短信和收信人信息
* @param code 策略编码
* @param strategy 策略
* @return map<code,list> << lst:<receivers,content>
*/
private Map getSms(Connection conn,String code,Send_strategy strategy){
Map map=new HashMap();
Statement st = null;
ResultSet rs = null;
String sqlCount="";
String content="";
try{
if("MRQKSB".equals(code)){
...
List<Receiver> receivers=getReceivers(conn,"","2");
List pageLst=new ArrayList();
pageLst.add(content);
pageLst.add(receivers);
lst.add(pageLst);
}
map.put(code,lst);
}else if("PFQYKCCLBJ".equals(code)){
sqlCount =getSql(code);
...
List<Receiver> receivers=getReceivers(conn,l0,"0");
List pageLst=new ArrayList();
pageLst.add(content);
pageLst.add(receivers);
lst.add(pageLst);
}
map.put(code,lst);
writeAlarm(conn,"0",strLst);
}else if("PFQYSBTX".equals(code)){
...
List<Receiver> receivers=getReceivers(conn,l0,"0");
List pageLst=new ArrayList();
pageLst.add(content);
pageLst.add(receivers);
lst.add(pageLst);
}
map.put(code,lst);
}else if("LSDWKCCLBJ".equals(code)){
...
List<Receiver> receivers=getReceivers(conn,l0,"1");
if(receivers.size()<1){
break;
}
List pageLst=new ArrayList();
pageLst.add(content);
pageLst.add(receivers);
lst.add(pageLst);

}
map.put(code,lst);
writeAlarm(conn,"1",strLst);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
if(rs!=null){
rs.close();
rs=null;
}
if(st!=null){
st.close();
st=null;
}
// if(conn!=null){
// conn.close();
// conn=null;
// }
} catch (SQLException e) {
e.printStackTrace();
}

}
return map;
}


/***
* 记录 频发单位、零售、超量报警
* @param type 0是公司 company 1是零售 station
* @param content
*/
private void writeAlarm(Connection conn,String type,List content){
...
}

/***
* 获取收信人信息
* @param comCode 对应编码
* @param type =0 公司code ; type=1 零售单位code ;2=报数对象
* @return
*/
private List<Receiver> getReceivers(Connection conn,String comCode,String type){
...
return lst;
}

/***
* 拼凑短信并完成发送
* @param code 对应code
*/
private void doWrite(Connection conn,String code,Map map) {
...
}
private void writeSMS(Connection conn,List<Receiver> rLst,String content){
...
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值