简体   繁体   English

在 Oracle 19c Java 存储过程中使用 java.nio 出现问题

[英]Problem with using java.nio in Oracle 19c Java Stored Procedures

I'm trying to connect to my localhost kafka server from java function saved into Oracle database 19.3.我正在尝试从保存到 Oracle 数据库 19.3 的 java function 连接到我的本地 kafka 服务器。 The problem is that i cannot reach the server.问题是我无法访问服务器。 In database trace files i see that kafka library is using java.nio package to connect to server.在数据库跟踪文件中,我看到 kafka 库正在使用 java.nio package 连接到服务器。 Any connection attempt is ending with "Connection refused".任何连接尝试都以“连接被拒绝”结束。 I admit also that I can send data to topics from command line tool.我也承认我可以从命令行工具向主题发送数据。

To check if my requests from database are incoming to localhost server at port 9092 i have run Hercules TCP Server and setup it to listen on this port.要检查来自数据库的请求是否传入端口 9092 的 localhost 服务器,我运行了 Hercules TCP 服务器并将其设置为侦听此端口。 Then when i'm using my java function it nothing happens.然后,当我使用我的 java function 时,什么也没有发生。 I write some "test" functions to make only simple connection to my local server, to see if there is some network problem.我编写了一些“测试”函数来只简单地连接到我的本地服务器,看看是否有一些网络问题。 The function where i use java.net package is working and i can see that i receive connection requests from database, but the function where i use java.nio package is returning always "Connection refused" The function where i use java.net package is working and i can see that i receive connection requests from database, but the function where i use java.nio package is returning always "Connection refused"

I have granted java.net.SocketPermission to my database user:我已将 java.net.SocketPermission 授予我的数据库用户:

exec dbms_java.grant_permission( 'KAFKA', 'SYS:java.net.SocketPermission', '*', 'connect,resolve' );
commit;

Are there needed some special permissions to use java.nio package into oracle database java functions or maybe i'm doing something wrong? Are there needed some special permissions to use java.nio package into oracle database java functions or maybe i'm doing something wrong?

Here are my java "test" functions code:这是我的 java “测试”功能代码:

CREATE OR REPLACE AND RESOLVE JAVA SOURCE NAMED "testTCP" AS
import java.net.Socket;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;

public class testTCP {

    public static String conn_nio(){
        
        String response;
        try {
            InetSocketAddress hostAddress = new InetSocketAddress("localhost", 9092);
            SocketChannel client = SocketChannel.open(hostAddress);
            client.close();
            response = "OK";
        }
        catch(Exception e){
            response = "Message: " + e.getMessage() + " Cause: " + e.getCause();
        }
        return response;
    }

    public static String conn_net() {
        String response;
        try
        {
            Socket socket = new Socket( "localhost", 9092 );
            socket.close();
            response = "OK";
        }
        catch( Exception e )
        {
            response = "Message: " + e.getMessage() + " Cause: " + e.getCause();
        }
        
        return response;

    }

}

Cannot reproduce.无法重现。 I pointed it to port 8080 of a local HTTP server which I started with我将它指向我开始使用的本地 HTTP 服务器的端口 8080

python -m SimpleHTTPServer 8080

and both connection methods are ok:两种连接方法都可以:

CREATE OR REPLACE FUNCTION test_net RETURN VARCHAR2 AS
LANGUAGE JAVA NAME 'testTCP.conn_net() return java.lang.String';
/

CREATE OR REPLACE FUNCTION test_nio RETURN VARCHAR2 AS
LANGUAGE JAVA NAME 'testTCP.conn_nio() return java.lang.String';
/

SELECT test_net FROM DUAL;
OK

SELECT test_nio FROM DUAL;
OK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM