關於java執行緒釋放資源問題
我於執行執行緒的時間出現這個錯誤
從一些資料上看到說是超過最大遊標,需要修改程式碼
把迴圈改成外迴圈,可改成外迴圈的話,那我連線是無法獲取資料庫的資料
出現ORA-01000
java.sql.SQLException: ORA-01000: maximum open cursors exceeded
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.Oopen.receive(Oopen.java:120)
at oracle.jdbc.ttc7.TTC7Protocol.open(TTC7Protocol.java:611)
at oracle.jdbc.driver.OracleStatement.open(OracleStatement.java:575)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2803)
at oracle.jdbc.driver.OracleStatement.executeQuery(OracleStatement.java:640)
at cn.soyu.sender.SendMailTest_G.sendMail(SendMailTest_G.java:107)
at cn.soyu.sender.SendMailTest_G.main(SendMailTest_G.java:269)
下面是我的程式碼,
請大家幫我看看
紅字的部分資料說要改成外迴圈
程式碼
/*
* Created on 2004-12-28
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package cn.soyu.sender;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import cn.soyu.sender.comm.Email_Autherticatorbean;
/**
* @author singlebin
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SendMailTest_G
{
public void sendMail(int year,int month)
{
System.out.println("Action start at:" + new Date().getTime());
String riqi1 = ""; //要查詢的月份時間
if (month < 10)
{
riqi1 = riqi1.valueOf(year) + "0" + riqi1.valueOf(month);
}
else
{
riqi1 = riqi1.valueOf(year) + riqi1.valueOf(month);
}
String riqi2 = ""; //要查詢的月份時間的下一個月的時間
if (month > 0 & month < 9)
{
riqi2 = riqi2.valueOf(year) + "0" + riqi2.valueOf(month + 1);
}
else if (month > 9 & month < 12)
{
riqi2 = riqi2.valueOf(year) + riqi2.valueOf(month + 1);
}
else
{
riqi2 = riqi2.valueOf(year + 1) + "0" + riqi2.valueOf(1);
}
try
{
//連線資料庫,得到連線
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager
.getConnection("jdbcracle:thin:@127.0.0.1:1521:billing",
"billing", "abcdef");
System.out.println("getConnection");
//按年月得到該月中使用過業務的每個使用者的手機號
String sqlMobile = "select distinct acct_mobile_num from g_gd165_local where"
+ " charge_date > '" + riqi1 + "' and charge_date < '" + riqi2 + "'"
+ " and rownum<1000000000 "+" and acct_locate_code = 756 "
+" and acct_mobile_num>'13150000000' and acct_mobile_num<'13200000001'" ;
System.out.println("sqlMobile = " +"\n"+ sqlMobile);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlMobile);
//rs:手機號
while (rs.next())
{
String mobile = null;
mobile = rs.getString("acct_mobile_num");
//得到彙總
StringBuffer sqlBufAccount = new StringBuffer();
//輸出彙總資訊
StringBuffer account = new StringBuffer();
account
.append("使用者:").append("\n")
.append(" 如下").append("\n")
.append(mobile).append("\n")
.append("---------------------------------------------------------------------------").append("\n")
.append("使用者:").append(mobile).append(" 手機號碼:").append(mobile)
.append(" 計費月份:").append(riqi1).append("\n");
//得到詳單
StringBuffer sqlBufDetail = new StringBuffer();
sqlBufDetail.append("select rownum,oppose_number,")
.append("org_trm_id ,charge_date,charge_time,call_duration,")
.append("charge,").append("oppose_area_code from g_gd165_local_756 where charge_date > '")
.append(riqi1).append("'and charge_date < '").append(riqi2).append("' and acct_mobile_num ='")
.append(mobile).append("'").append(" and rownum<5000");
System.out.println("得到詳單 = "+"\n"+sqlBufDetail.toString());
Connection con = null;
con = DriverManager
.getConnection("jdbcracle:thin:@127.0.0.1:1521:billing",
"billing", "abcdef");
Statement stm = conn.createStatement();
ResultSet rs3 = null;
rs3 = stm.executeQuery(sqlBufDetail.toString());
//顯示詳單資訊
StringBuffer detail = new StringBuffer();
detail
.append("-------------------------------------------------------------------------------").append("\n")
.append(" NO. ").append(" ").append("對方號碼 ").append(" ").append("型別 ").append(" ").append("日 期")
.append(" ").append("時 間").append(" ").append("時長(秒)").append(" ").append("話費(元) ").append(" ")
.append("通話地").append("\n")
.append("-------------------------------------------------------------------------------").append("\n");
while(rs3.next())
{
String org1=null;
if(rs3.getString("oppose_number")==null)
org1 = "0";
else
org1=rs3.getString("oppose_number");
detail
.append(StringUtil.padString(rs3.getString("rownum"),' ',3,StringUtil.LEFT))
.append(" ")
.append(StringUtil.padString(org1,' ',18,StringUtil.LEFT))
.append(" ")
.append(StringUtil.padString(org,' ',4,StringUtil.LEFT)).append("")
.append(" ")
.append(StringUtil.padString(rs3.getString("charge_date"),' ',8,StringUtil.LEFT))
.append(" ")
.append(StringUtil.padString(rs3.getString("charge_time"),' ',4,StringUtil.LEFT))
.append(" ")
.append(StringUtil.padString(String.valueOf(rs3.getInt("call_duration")),' ',4,StringUtil.LEFT))
.append("\t")
.append(StringUtil.padString(String.valueOf(rs3.getFloat("charge")),' ',4,StringUtil.LEFT))
.append("\t")
.append(StringUtil.padString(rs3.getString("oppose_area_code"),' ',4,StringUtil.LEFT))
.append("\n");
}
if(rs3 != null)
rs3 = null;
detail
.append("-------------------------------------------------------------------------------").append("\n") System.out.println("詳單資訊 = "+"\n"+detail.toString());
sendMain(account.toString()+detail.toString(),mobile+"@gd165.com");
}
下面這段程式碼有人說需要改成外迴圈
《《
if(rs!=null)
{
rs.close();
rs = null;
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (java.sql.SQLException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
》》
System.out.println("Action end at:" + new Date().getTime());
}
public static void sendMain(String content1, String to1)
{
//收件人
String to = null;
//發件人
String from = null;
//主題
String subject = null;
//抄送人
String cc = null;
//暗抄送
String bcc = null;
// mail 主機
String mailhost = null;
// mail 內容
String content = null;
//MIME郵件物件
MimeMessage mimeMsg = null;
//郵件會話物件
Session session = null;
//************ 不同之處 *************/
String user = null;
String password = null;
try
{
mailhost = "mail";
from = "singlebin@163.com";
to = to1;
subject = "";
content = content1;
//content = "這是個帶身份驗證的JavMail!"+"\n"+"This is a test";
user = "support";
password = "mail";
Properties props = System.getProperties(); //獲得系統屬性
props.put("mail.smtp.host", mailhost); //設定SMTP主機
props.put("mail.smtp.auth", "true"); //設定身份驗證為真,若須身份驗證則必須設為真
//獲得郵件會話物件
//session = Session.getDefaultInstance(props,null);
//第二個引數為身份驗證
session = Session
.getDefaultInstance(props, new Email_Autherticatorbean(user, password));
//建立MIME郵件物件
mimeMsg = new MimeMessage(session);
//設定發信人
mimeMsg.setFrom(new InternetAddress(from));
//設定收信人
if (to != null)
{
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
}
//設定抄送人
if (cc != null)
{
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
}
//設定暗送人
if (bcc != null)
{
mimeMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
}
//設定郵件主題
mimeMsg.setSubject(subject, "GBK");
//設定郵件內容
mimeMsg.setText(content, "GBK");
//傳送日期
mimeMsg.setSentDate(new Date());
//傳送郵件
Transport.send(mimeMsg);
//System.out.println( "Email send!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
SendMailTest_G test = new SendMailTest_G();
test.sendMail(2004,11);
}
}
從一些資料上看到說是超過最大遊標,需要修改程式碼
把迴圈改成外迴圈,可改成外迴圈的話,那我連線是無法獲取資料庫的資料
出現ORA-01000
java.sql.SQLException: ORA-01000: maximum open cursors exceeded
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.Oopen.receive(Oopen.java:120)
at oracle.jdbc.ttc7.TTC7Protocol.open(TTC7Protocol.java:611)
at oracle.jdbc.driver.OracleStatement.open(OracleStatement.java:575)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2803)
at oracle.jdbc.driver.OracleStatement.executeQuery(OracleStatement.java:640)
at cn.soyu.sender.SendMailTest_G.sendMail(SendMailTest_G.java:107)
at cn.soyu.sender.SendMailTest_G.main(SendMailTest_G.java:269)
下面是我的程式碼,
請大家幫我看看
紅字的部分資料說要改成外迴圈
程式碼
/*
* Created on 2004-12-28
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package cn.soyu.sender;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import cn.soyu.sender.comm.Email_Autherticatorbean;
/**
* @author singlebin
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class SendMailTest_G
{
public void sendMail(int year,int month)
{
System.out.println("Action start at:" + new Date().getTime());
String riqi1 = ""; //要查詢的月份時間
if (month < 10)
{
riqi1 = riqi1.valueOf(year) + "0" + riqi1.valueOf(month);
}
else
{
riqi1 = riqi1.valueOf(year) + riqi1.valueOf(month);
}
String riqi2 = ""; //要查詢的月份時間的下一個月的時間
if (month > 0 & month < 9)
{
riqi2 = riqi2.valueOf(year) + "0" + riqi2.valueOf(month + 1);
}
else if (month > 9 & month < 12)
{
riqi2 = riqi2.valueOf(year) + riqi2.valueOf(month + 1);
}
else
{
riqi2 = riqi2.valueOf(year + 1) + "0" + riqi2.valueOf(1);
}
try
{
//連線資料庫,得到連線
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager
.getConnection("jdbcracle:thin:@127.0.0.1:1521:billing",
"billing", "abcdef");
System.out.println("getConnection");
//按年月得到該月中使用過業務的每個使用者的手機號
String sqlMobile = "select distinct acct_mobile_num from g_gd165_local where"
+ " charge_date > '" + riqi1 + "' and charge_date < '" + riqi2 + "'"
+ " and rownum<1000000000 "+" and acct_locate_code = 756 "
+" and acct_mobile_num>'13150000000' and acct_mobile_num<'13200000001'" ;
System.out.println("sqlMobile = " +"\n"+ sqlMobile);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sqlMobile);
//rs:手機號
while (rs.next())
{
String mobile = null;
mobile = rs.getString("acct_mobile_num");
//得到彙總
StringBuffer sqlBufAccount = new StringBuffer();
//輸出彙總資訊
StringBuffer account = new StringBuffer();
account
.append("使用者:").append("\n")
.append(" 如下").append("\n")
.append(mobile).append("\n")
.append("---------------------------------------------------------------------------").append("\n")
.append("使用者:").append(mobile).append(" 手機號碼:").append(mobile)
.append(" 計費月份:").append(riqi1).append("\n");
//得到詳單
StringBuffer sqlBufDetail = new StringBuffer();
sqlBufDetail.append("select rownum,oppose_number,")
.append("org_trm_id ,charge_date,charge_time,call_duration,")
.append("charge,").append("oppose_area_code from g_gd165_local_756 where charge_date > '")
.append(riqi1).append("'and charge_date < '").append(riqi2).append("' and acct_mobile_num ='")
.append(mobile).append("'").append(" and rownum<5000");
System.out.println("得到詳單 = "+"\n"+sqlBufDetail.toString());
Connection con = null;
con = DriverManager
.getConnection("jdbcracle:thin:@127.0.0.1:1521:billing",
"billing", "abcdef");
Statement stm = conn.createStatement();
ResultSet rs3 = null;
rs3 = stm.executeQuery(sqlBufDetail.toString());
//顯示詳單資訊
StringBuffer detail = new StringBuffer();
detail
.append("-------------------------------------------------------------------------------").append("\n")
.append(" NO. ").append(" ").append("對方號碼 ").append(" ").append("型別 ").append(" ").append("日 期")
.append(" ").append("時 間").append(" ").append("時長(秒)").append(" ").append("話費(元) ").append(" ")
.append("通話地").append("\n")
.append("-------------------------------------------------------------------------------").append("\n");
while(rs3.next())
{
String org1=null;
if(rs3.getString("oppose_number")==null)
org1 = "0";
else
org1=rs3.getString("oppose_number");
detail
.append(StringUtil.padString(rs3.getString("rownum"),' ',3,StringUtil.LEFT))
.append(" ")
.append(StringUtil.padString(org1,' ',18,StringUtil.LEFT))
.append(" ")
.append(StringUtil.padString(org,' ',4,StringUtil.LEFT)).append("")
.append(" ")
.append(StringUtil.padString(rs3.getString("charge_date"),' ',8,StringUtil.LEFT))
.append(" ")
.append(StringUtil.padString(rs3.getString("charge_time"),' ',4,StringUtil.LEFT))
.append(" ")
.append(StringUtil.padString(String.valueOf(rs3.getInt("call_duration")),' ',4,StringUtil.LEFT))
.append("\t")
.append(StringUtil.padString(String.valueOf(rs3.getFloat("charge")),' ',4,StringUtil.LEFT))
.append("\t")
.append(StringUtil.padString(rs3.getString("oppose_area_code"),' ',4,StringUtil.LEFT))
.append("\n");
}
if(rs3 != null)
rs3 = null;
detail
.append("-------------------------------------------------------------------------------").append("\n") System.out.println("詳單資訊 = "+"\n"+detail.toString());
sendMain(account.toString()+detail.toString(),mobile+"@gd165.com");
}
下面這段程式碼有人說需要改成外迴圈
《《
if(rs!=null)
{
rs.close();
rs = null;
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (java.sql.SQLException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
》》
System.out.println("Action end at:" + new Date().getTime());
}
public static void sendMain(String content1, String to1)
{
//收件人
String to = null;
//發件人
String from = null;
//主題
String subject = null;
//抄送人
String cc = null;
//暗抄送
String bcc = null;
// mail 主機
String mailhost = null;
// mail 內容
String content = null;
//MIME郵件物件
MimeMessage mimeMsg = null;
//郵件會話物件
Session session = null;
//************ 不同之處 *************/
String user = null;
String password = null;
try
{
mailhost = "mail";
from = "singlebin@163.com";
to = to1;
subject = "";
content = content1;
//content = "這是個帶身份驗證的JavMail!"+"\n"+"This is a test";
user = "support";
password = "mail";
Properties props = System.getProperties(); //獲得系統屬性
props.put("mail.smtp.host", mailhost); //設定SMTP主機
props.put("mail.smtp.auth", "true"); //設定身份驗證為真,若須身份驗證則必須設為真
//獲得郵件會話物件
//session = Session.getDefaultInstance(props,null);
//第二個引數為身份驗證
session = Session
.getDefaultInstance(props, new Email_Autherticatorbean(user, password));
//建立MIME郵件物件
mimeMsg = new MimeMessage(session);
//設定發信人
mimeMsg.setFrom(new InternetAddress(from));
//設定收信人
if (to != null)
{
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
}
//設定抄送人
if (cc != null)
{
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
}
//設定暗送人
if (bcc != null)
{
mimeMsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc));
}
//設定郵件主題
mimeMsg.setSubject(subject, "GBK");
//設定郵件內容
mimeMsg.setText(content, "GBK");
//傳送日期
mimeMsg.setSentDate(new Date());
//傳送郵件
Transport.send(mimeMsg);
//System.out.println( "Email send!");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
SendMailTest_G test = new SendMailTest_G();
test.sendMail(2004,11);
}
}
相關文章
- 關於js執行緒問題的解讀JS執行緒
- 多執行緒問題解釋執行緒
- 多執行緒相關問題執行緒
- 深入JAVA執行緒安全問題Java執行緒
- Java多執行緒中執行緒安全與鎖問題Java執行緒
- 有個關於多執行緒的識別問題執行緒
- java基礎 關於執行緒安全Java執行緒
- 05.java多執行緒問題Java執行緒
- J2SE - 關於SimpleDateFonnat的執行緒安全問題執行緒
- 深入解析 TiFlash丨多併發下執行緒建立、釋放的阻塞問題執行緒
- Java中解決多執行緒資料安全問題Java執行緒
- 關於執行緒池的面試題執行緒面試題
- Java 執行緒安全問題的本質Java執行緒
- Java併發(11)- 有關執行緒池的10個問題Java執行緒
- Java多執行緒面試高配問題---多執行緒(3)🧵Java執行緒面試
- 關於Java多執行緒的執行緒同步和執行緒通訊的一些小問題(順便分享幾篇高質量的博文)Java執行緒
- java 多執行緒(關於Thread的講解)Java執行緒thread
- Java 關於執行緒的一些使用Java執行緒
- 40 個 Java 多執行緒問題總結Java執行緒
- Java多執行緒和併發問題集Java執行緒
- fastapi 關於執行緒池、同步、非同步的問題解答彙總ASTAPI執行緒非同步
- Java 執行緒和 volatile 解釋Java執行緒
- 03 執行緒安全問題執行緒
- SimpleDateFormat 執行緒安全問題ORM執行緒
- 04.關於執行緒你必須知道的8個問題(下)執行緒
- 03.關於執行緒你必須知道的8個問題(中)執行緒
- Java之執行緒安全問題的3種處理方式(通過執行緒同步)Java執行緒
- 關於程式與執行緒的一個簡單解釋執行緒
- 關於Java併發多執行緒的一點思考Java執行緒
- 使用redis分散式鎖解決併發執行緒資源共享問題Redis分散式執行緒
- 面試中關於多執行緒同步,你必須要思考的問題面試執行緒
- Java面試中,一些常見的有關多執行緒問題!Java面試執行緒
- 面試題-關於Java執行緒池一篇文章就夠了面試題Java執行緒
- java try(){}catch(){}自動資源釋放Java
- 詳解JAVA執行緒問題診斷工具Thread DumpJava執行緒thread
- Java多執行緒開發|volatile與偽共享問題Java執行緒
- ArrayList 的執行緒安全問題執行緒
- JUC之多執行緒鎖問題執行緒
- java執行緒的筆試題Java執行緒筆試