Java oracle更改通知问题
我有oracle database 11.2,我正在尝试让oracle更改通知正常工作。但它似乎没有通知我,我正在使用ojdbc6.jar。表正在注册到通知中;这是通过运行Java oracle更改通知问题,java,oracle,Java,Oracle,我有oracle database 11.2,我正在尝试让oracle更改通知正常工作。但它似乎没有通知我,我正在使用ojdbc6.jar。表正在注册到通知中;这是通过运行 select * from USER_CHANGE_NOTIFICATION_REGS 当我从外部客户端对表执行更新时,我的侦听器没有被调用。但当我关闭/启动数据库时,它会发出通知 import java.sql.ResultSet; import java.sql.SQLException; import java.sq
select * from USER_CHANGE_NOTIFICATION_REGS
当我从外部客户端对表执行更新时,我的侦听器没有被调用。但当我关闭/启动数据库时,它会发出通知
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleDriver;
import oracle.jdbc.OracleStatement;
import oracle.jdbc.dcn.DatabaseChangeEvent;
import oracle.jdbc.dcn.DatabaseChangeListener;
import oracle.jdbc.dcn.DatabaseChangeRegistration;
public class DBChangeNotification
{
static final String USERNAME= "scott";
static final String PASSWORD= "tiger";
static String URL;
public static void main(String[] argv)
{
if(argv.length < 1)
{
System.out.println("Error: You need to provide the URL in the first argument.");
System.out.println(" For example: > java -classpath .:ojdbc5.jar DBChangeNotification \"jdbc:oracle:thin: @(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=yourhost.yourdomain.com)(PORT=1521)) CONNECT_DATA=(SERVICE_NAME=yourservicename)))\"");
System.exit(1);
}
URL = argv[0];
DBChangeNotification demo = new DBChangeNotification();
try
{
demo.run();
}
catch(SQLException mainSQLException )
{
mainSQLException.printStackTrace();
}
}
void run() throws SQLException
{
OracleConnection conn = connect();
// first step: create a registration on the server:
Properties prop = new Properties();
// if connected through the VPN, you need to provide the TCP address of the client.
// For example:
// prop.setProperty(OracleConnection.NTF_LOCAL_HOST,"14.14.13.12");
// Ask the server to send the ROWIDs as part of the DCN events (small performance
// cost):
prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS,"true");
//
//Set the DCN_QUERY_CHANGE_NOTIFICATION option for query registration with finer granularity.
prop.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION,"true");
// The following operation does a roundtrip to the database to create a new
// registration for DCN. It sends the client address (ip address and port) that
// the server will use to connect to the client and send the notification
// when necessary. Note that for now the registration is empty (we haven't registered
// any table). This also opens a new thread in the drivers. This thread will be
// dedicated to DCN (accept connection to the server and dispatch the events to
// the listeners).
DatabaseChangeRegistration dcr = conn.registerDatabaseChangeNotification(prop);
try
{
// add the listenerr:
DCNDemoListener list = new DCNDemoListener(this);
dcr.addListener(list);
// second step: add objects in the registration:
Statement stmt = conn.createStatement();
// associate the statement with the registration:
((OracleStatement)stmt).setDatabaseChangeRegistration(dcr);
ResultSet rs = stmt.executeQuery("select rowid from dept where 1 = 2");
while (rs.next())
{}
String[] tableNames = dcr.getTables();
for(int i=0;i<tableNames.length;i++)
System.out.println(tableNames[i]+" is part of the registration.");
rs.close();
stmt.close();
}
catch(SQLException ex)
{
// if an exception occurs, we need to close the registration in order
// to interrupt the thread otherwise it will be hanging around.
if(conn != null)
conn.unregisterDatabaseChangeNotification(dcr);
throw ex;
}
finally
{
try
{
// Note that we close the connection!
conn.close();
}
catch(Exception innerex){ innerex.printStackTrace(); }
}
synchronized( this )
{
// wait until we get the event
try{ this.wait();} catch( InterruptedException ie ) {}
}
// At the end: close the registration (comment out these 3 lines in order
// to leave the registration open).
OracleConnection conn3 = connect();
conn3.unregisterDatabaseChangeNotification(dcr);
conn3.close();
}
/**
* Creates a connection the database.
*/
OracleConnection connect() throws SQLException
{
OracleDriver dr = new OracleDriver();
Properties prop = new Properties();
prop.setProperty("user",DBChangeNotification.USERNAME);
prop.setProperty("password",DBChangeNotification.PASSWORD);
return (OracleConnection)dr.connect(DBChangeNotification.URL,prop);
}
}
/**
* DCN listener: it prints out the event details in stdout.
*/
class DCNDemoListener implements DatabaseChangeListener
{
DBChangeNotification demo;
DCNDemoListener(DBChangeNotification dem)
{
demo = dem;
}
public void onDatabaseChangeNotification(DatabaseChangeEvent e)
{
Thread t = Thread.currentThread();
System.out.println("DCNDemoListener: got an event ("+this+" running on thread "+t+")");
System.out.println(e.toString());
synchronized( demo ){ demo.notify();}
}
}
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Statement;
导入java.util.Properties;
导入oracle.jdbc.OracleConnection;
导入oracle.jdbc.OracleDriver;
导入oracle.jdbc.oracle语句;
导入oracle.jdbc.dcn.DatabaseChangeEvent;
导入oracle.jdbc.dcn.DatabaseChangeListener;
导入oracle.jdbc.dcn.DatabaseChangeRegistration;
公共类DBChangeNotification
{
静态最终字符串USERNAME=“scott”;
静态最终字符串PASSWORD=“tiger”;
静态字符串URL;
公共静态void main(字符串[]argv)
{
如果(argv.length<1)
{
System.out.println(“错误:您需要在第一个参数中提供URL。”);
System.out.println(“例如:>java-classpath:ojdbc5.jar DBChangeNotification\”jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=yourhost.yourdomain.com)(PORT=1521))CONNECT\u DATA=(SERVICE\u NAME=yourservicename))\”;
系统出口(1);
}
URL=argv[0];
DBChangeNotification demo=新的DBChangeNotification();
尝试
{
demo.run();
}
捕获(SQLException mainSQLException)
{
mainSQLException.printStackTrace();
}
}
void run()引发SQLException
{
OracleConnection conn=connect();
//第一步:在服务器上创建注册:
Properties prop=新属性();
//如果通过VPN连接,则需要提供客户端的TCP地址。
//例如:
//prop.setProperty(OracleConnection.NTF_LOCAL_HOST,“14.14.13.12”);
//要求服务器将rowid作为DCN事件的一部分发送(小性能)
//费用):
prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS,“true”);
//
//为更细粒度的查询注册设置DCN\u QUERY\u CHANGE\u通知选项。
prop.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION,“true”);
//下面的操作执行到数据库的往返以创建新数据库
//注册DCN。它发送
//服务器将使用连接到客户端并发送通知
//请注意,目前注册为空(我们尚未注册)
//这还会在驱动程序中打开一个新线程。此线程将
//专用于DCN(接受到服务器的连接并将事件分派到
//听众)。
DatabaseChangeRegistration dcr=conn.registerDatabaseChangeNotification(prop);
尝试
{
//添加侦听器:
DCNDemollistener list=新的DCNDemollistener(此);
dcr.addListener(列表);
//第二步:在注册中添加对象:
语句stmt=conn.createStatement();
//将声明与注册关联:
(OracleStatement)stmt.setDatabaseChangeRegistration(dcr);
ResultSet rs=stmt.executeQuery(“从dept中选择rowid,其中1=2”);
while(rs.next())
{}
字符串[]tableNames=dcr.getTables();
对于(int i=0;i嗯,这个更改通知根本不起作用。我将使用另一个路由Oracle Advance Queue JMS消息传递
oracle.jms.AQjmsSession
oracle.jms.AQjmsFactory
您是否将JOB\u QUEUE\u PROCESS
init.ora参数设置为大于零的值?