如何保持oracle中的socket连接??? 在我的项目中需要oracle数据库与web服务器进行网络通信,我是这样实现的,每当新数据插入后通过socket将其发送到web服务器上。这样做的问题是,每次触发器触发时都会new一个socket,结束后socket被销毁,这样很低效,因此我专门写了一个socket初始化函数,每次web服务器重启的时候就需要在oracle中执行一遍该函数,当然项目在实际工作过程中不会频繁重启,但是这毕竟不是个完美的解决方案,还有没有更好的方法?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 次触发器触发时都会new一个socket,结束后socket被销毁,这样很低效,你是不想让这个socket连接销毁吗?可以定时发送一个无用的数据,这样连接就不会销毁了,并且一直存在着--》长连接 可以创建多个socket,然后,有一个线程专门负责socket的定期ping操作,这样就不用销毁了。每次取一个idle的socket发送数据就行了。 感谢楼上两位的回复!确实是这个意思,而且你们的方法我也想到了,有些许不同,我再把问题的重点描述一遍:其实在初始化之后,socket就保持下去了,不会随着触发器的结束而销毁;而我担心的问题是一旦web服务器当掉,这个socket自然也就失效了,虽然没有销毁。为此我写了一段测试程序用来探测服务器,连接上之后,socket会保持下去,此时若服务器停止,该socket会转为探测直到重新连上服务器,这样就使得客户端一直使用同一个socket。客户端代码如下:import java.net.*;import java.io.*;public class SocketTest { static Socket socket; static BufferedReader reader; static PrintWriter out; static{ while(true){ try{ System.out.println("connecting~~~"); socket = new Socket(InetAddress.getByName(null), 9090); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())), true); System.out.println("success!"); break; }catch (IOException e) { System.out.println("failed!"); try{ Thread.sleep(1000); }catch(InterruptedException ie){} } } } public static void output(long i, long j){ out.println(i); out.println(j); } public static void test(){ Thread th = new Thread(new Runnable(){ public void run(){ while(true){ try{ String str = SocketTest.reader.readLine(); while(str != null){ System.out.println(str); str = SocketTest.reader.readLine(); } }catch(IOException e){} SocketTest.restart(); try{ Thread.sleep(1000); }catch(InterruptedException ie){} } } }); th.start(); } public static void restart(){ while(true){ try{ System.out.println("connecting~~~"); socket = new Socket(InetAddress.getByName(null), 9090); reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter( new BufferedWriter( new OutputStreamWriter( socket.getOutputStream())), true); System.out.println("success!"); break; }catch (IOException e) { System.out.println("failed!"); try{ Thread.sleep(1000); }catch(InterruptedException ie){} } } } /** * @param args */ public static void main(String[] args){ // TODO Auto-generated method stub long i = 0; long j = 0; SocketTest.test(); while(true){ SocketTest.output(i++, j++); //System.out.println(SocketTest.socket.isConnected()); try{ Thread.sleep(1000); }catch(InterruptedException ie){} } }}单独运行此程序是没有问题的。移植到oracle的java source里,去除main函数,将test函数置于static块中的while循环后,我期望也能达到同样效果,但是没有成功。那么应该如何改动呢?oracle里面的java程序是从哪里开始运行的呢? package 数据库迁移中关于大对象的问题(sql server 到oracle) 请教:如何获取数据库服务器(Oracle)的时间 一个关于批量分页查询的很奇怪的问题,诚请高手出马!!(有几个高手都没想明白的问题) 怎样给一个用户具有在存储工程中EXECUTE动态建表的权限? 怎样退出一个插入触发器。而不进行插入动作。 怎么回事,请高手看看! ORACLE导出问题 触发器语法正切 为什么执行不能通过? 数据库值如何替换 执行select都是返回空值,执行INSERT,UPDATE,DELETE 没有问题 帮忙看看这个job 在线等
可以定时发送一个无用的数据,这样连接就不会销毁了,并且一直存在着--》长连接
每次取一个idle的socket发送数据就行了。
其实在初始化之后,socket就保持下去了,不会随着触发器的结束而销毁;而我担心的问题是一旦web服务器当掉,这个socket自然也就失效了,虽然没有销毁。
为此我写了一段测试程序用来探测服务器,连接上之后,socket会保持下去,此时若服务器停止,该socket会转为探测直到重新连上服务器,这样就使得客户端一直使用同一个socket。客户端代码如下:
import java.net.*;
import java.io.*;public class SocketTest {
static Socket socket;
static BufferedReader reader;
static PrintWriter out;
static{
while(true){
try{
System.out.println("connecting~~~");
socket = new Socket(InetAddress.getByName(null), 9090);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
System.out.println("success!");
break;
}catch (IOException e) {
System.out.println("failed!");
try{
Thread.sleep(1000);
}catch(InterruptedException ie){}
}
}
}
public static void output(long i, long j){
out.println(i);
out.println(j);
}
public static void test(){
Thread th = new Thread(new Runnable(){
public void run(){
while(true){
try{
String str = SocketTest.reader.readLine();
while(str != null){
System.out.println(str);
str = SocketTest.reader.readLine();
}
}catch(IOException e){}
SocketTest.restart();
try{
Thread.sleep(1000);
}catch(InterruptedException ie){}
}
}
});
th.start();
}
public static void restart(){
while(true){
try{
System.out.println("connecting~~~");
socket = new Socket(InetAddress.getByName(null), 9090);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
System.out.println("success!");
break;
}catch (IOException e) {
System.out.println("failed!");
try{
Thread.sleep(1000);
}catch(InterruptedException ie){}
}
}
}
/**
* @param args
*/
public static void main(String[] args){
// TODO Auto-generated method stub
long i = 0;
long j = 0;
SocketTest.test();
while(true){
SocketTest.output(i++, j++);
//System.out.println(SocketTest.socket.isConnected());
try{
Thread.sleep(1000);
}catch(InterruptedException ie){}
}
}}
单独运行此程序是没有问题的。移植到oracle的java source里,去除main函数,将test函数置于static块中的while循环后,我期望也能达到同样效果,但是没有成功。
那么应该如何改动呢?
oracle里面的java程序是从哪里开始运行的呢?