【问题标题】:ORA-12519, TNS:no appropriate service handler found while inserting into Oracle Database with X threadsORA-12519,TNS:使用 X 线程插入 Oracle 数据库时未找到合适的服务处理程序
【发布时间】:2013-01-21 04:06:32
【问题描述】:

我正在尝试插入具有两列的 Oracle 数据库-

ID         Primary Key     varchar2 (4000)
ACCOUNT                    varchar2 (4000)

我为此编写了一个多线程程序。每个线程每次都使用unique id 插入ID column,因为ID 是主键。

我在某些时候面临的唯一问题是 - 下面的代码在运行几秒钟后抛出以下异常。

 1) Null Pointer Exception
 2) java.sql.SQLException: Listener refused the connection with the following error:ORA-12519, TNS:no appropriate service handler found

我无法在我的代码中找到此问题的任何根本原因,因为对我来说一切都很好。当我正确关闭每个连接时。那么这个 NPE 是如何被抛出的以及其他异常呢?

    ExecutorService service = Executors.newFixedThreadPool(10);

    try {
        // queue some tasks
        for (int i = 0; i < 100 * 10; i++) {
            service.submit(new ThreadTask());
        }
        service.shutdown();
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);

        while (!service.isTerminated()) {

        }
    } catch (InterruptedException e) {
        LOG.warn("Threw a Interrupted Exception in" + XMPLoadTest.class.getSimpleName()
                + ".XMPLoadTest: boss told me to stop...Not my fault!!");
    }

下面是ThreadTask类-

class ThreadTask implements Runnable {

    private static final String DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static final String CONNECTION = "jdbc:oracle:thin:@localhost:1521:ORCL";
    private static final String USER = "scott";
    private static final String PASSWORD = "tiger";
    private static Connection dbConnection = null;
    private static PreparedStatement preparedStatement = null;

    private static final AtomicInteger id = new AtomicInteger(1);

    private final static Logger LOG = Logger.getLogger(ThreadTask.class.getName());

    public ThreadTask() {

    }

    @Override
    public void run() {

        try {

            dbConnection = getDBConnection();
            preparedStatement = dbConnection.prepareStatement(Constants.INSERT_ORACLE_SQL);

            preparedStatement.setString(1, String.valueOf(id.getAndIncrement()));
            preparedStatement.setString(2, Constants.A_ACCOUNT);

            preparedStatement.executeUpdate();

        } catch (Exception e) {
            // NPE getting thrown here/And second exception as well
            LOG.error("Threw a SQLException in " + getClass().getSimpleName(), e);
        } finally {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                    preparedStatement = null;
                } catch (SQLException e) {
                    //Oouch...
                    LOG.error("Threw a SQLException in finally block of prepared statement " + getClass().getSimpleName(), e);
                }
            }
            if (dbConnection != null) {
                try {
                    dbConnection.close();
                    dbConnection = null;
                } catch (SQLException e) {
                    //Better go and look for SQL.
                    LOG.error("Threw a SQLException in finally block of dbConnection " + getClass().getSimpleName(), e);
                }
            }
        }
    }

    /**
     * Attempts to establish a connection to the given database URL
     * 
     * @return the db connection
     */
    private Connection getDBConnection() {

        Connection dbConnection = null;

        try {
            Class.forName(XMP_DRIVER);
            dbConnection = DriverManager.getConnection(CONNECTION, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            LOG.error("Threw a ClassNotFoundException in " + getClass().getSimpleName(), e);
        } catch (SQLException e) {
            //DAMN! I'm not....
            LOG.error("Threw a SQLException in " + getClass().getSimpleName(), e);
        } catch (Exception e) {
            LOG.error("Threw a Exception in " + getClass().getSimpleName(), e);
        }

        return dbConnection;
    }
}

我的代码有任何潜在问题吗?我更担心这个 NPE。

堆栈跟踪:

19:14:28,372 ERROR ThreadTask:187 - Threw a SQLException in ThreadTask
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12519, TNS:no appropriate service handler found

    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:458)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
    at java.sql.DriverManager.getConnection(DriverManager.java:322)
    at java.sql.DriverManager.getConnection(DriverManager.java:358)
    at com.ebay.xmp.lnp.ThreadTask.getDBConnection(XMPLoadTest.java:179)
    at com.ebay.xmp.lnp.ThreadTask.run(XMPLoadTest.java:137)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)
Caused by: oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12519, TNS:no appropriate service handler found

    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:395)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1102)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:320)
    ... 14 more
19:14:28,376 ERROR ThreadTask:139 - Threw a SQLException in ThreadTask
java.lang.NullPointerException
    at com.ebay.xmp.lnp.ThreadTask.run(XMPLoadTest.java:137)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:736)

【问题讨论】:

  • 您使用的是哪个版本的 oracle?这个java代码看起来不错。似乎您的 oracle 设置有问题。您是否尝试只为 1 个线程运行该程序(没有太多线程)?这个链接可能对你也有用dadbm.com/2011/11/…
  • 感谢 Prateek 的帮助。我忘记了 Oracle 版本,但我认为它是 11g or 10g 关于我的第一个异常 Null Pointer Exception 有什么想法吗?什么阶段会出现?与此同时,我会看看你提供给我的文章。
  • 您没有给出发生此错误的确切位置,但很可能是由于从 getDBConnection 方法返回的空 dbConnection。
  • 请使用空指针异常的堆栈跟踪更新您的答案。我可以假设的一件事是变量 dbConnection 在竞争条件下被设置为 null,而另一个线程试图在其上运行 preparedStatement.executeUpdate()
  • 我也用 Stacktrace 更新了我的问题。我也相信它只是因为dbConnection null 而发生的,我很困惑 dbConnection 是如何为空的?这可能是一个竞争条件。我确实进行了调试,并发现 dbConnection 返回 null。但这怎么可能?我们如何解决这个问题?

标签: java multithreading oracle executorservice ora-12519


【解决方案1】:

我刚刚遇到了相同的场景。

我的先决条件: 我在 Eclipse IDE 中处理一个包含插入操作的简单 Hibernate 程序。

当通过我的 HIBERNATE 程序进行 INSERTION 时,此异常曾经发生在某些输入上,而在其他一些输入上则没有发生。 是的,它听起来既愚蠢又奇怪。

我很困惑,我确实在 google n stack over flow 等上查看了答案。 这是一个非常简单的观察,拯救了我一整天!

如果您在 Eclipse 中工作并且遇到类似的情况... 解决方案 : 在 Eclipse 中,我只是在“控制台”选项卡中关闭了所有打开的控制台,这些控制台是在我多次执行同一程序时构建的。他们被堆叠起来,正如预期的那样,空间不足。关闭所有这些解决了我的问题。 这听起来确实是一个非常模糊的解决方案。 没关系,它只是观察为什么会发生这种情况并且异常没有再次发生。

The Eclipse IDE Image

【讨论】:

    【解决方案2】:

    竞态条件是最简单的解释,因为在当前代码中保证dbConnection 在多个线程运行时作为非空值存在。

    简单地说,每个ThreadTask 对象都可以访问static Connection dbConnection 类字段。如果进行评估,我们认为在应用程序启动期间启动了 3 个线程,并且会发生以下情况:

    Main+----------------------------------------------------------------------(T)
        |+Thread1 --dbConnection=getDBConnection()-------------dbConnection=null|
        |                                                                       |
        |----------+Thread2 --dbConnection=getDBConnection()--------------------|dbConnection=null
        |
        |-------------+Thread3--dbConnection=getDBConnection()------------------|preparedStatement.executeUpdate()
    

    在时间 (T),由于 Thread1 将静态变量设置为 null,因此您的 Thread3 将抛出异常。

    * 更新 *

    你需要使用连接池,查看c3p0

    * 结束更新 *

    【讨论】:

    • 感谢 Vijay 让我了解这种情况。那么我该如何解决这个问题呢?所以这意味着我应该使用类似private Connection dbConnection = null; private PreparedStatement preparedStatement = null; 的东西,对吧?而不是使用static connection and statement?
    • 更新了我的答案以进行澄清。
    • 感谢 Vijay 的更新。除了连接池。应该有其他方法来避免这种竞争条件吧?
    • Java 不是我的日常工作语言,但我想ThreadLocal 应该为您提供一个选项,如果您不研究 c3p0。查看ThreadLocalibm.com/developerworks/java/library/j-threads3/index.html
    • 该更新,即使解决了问题,也无法解释原因。
    猜你喜欢
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 2013-01-16
    • 2016-07-26
    • 1970-01-01
    • 2019-05-06
    相关资源
    最近更新 更多