声明结束后不允许任何操作 [英] No operations allowed after statement closed

查看:153
本文介绍了声明结束后不允许任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到Exception,签名为语句关闭后不允许任何操作。在我的Java代码中,我试图将值插入数据库。错误签名说我的Statement对象被关闭了,我试图在我的代码中再次使用它,但我正在努力理解的是为什么会发生这种情况,因为我没有关闭代码中的任何连接。

I am getting the Exception with the signature No operations allowed after statement closed. inside my Java code where I am trying to insert values into the database. The error signature says that my Statement object gets closed and I am trying to use it again in my code , but what I am struggling to understand is why is this happening as I am not closing any connections anywhere in my code.

这是Java代码。

public class DataBaseAccessUtils {

    private static String jdbcUrl = 
            AppConfig.findMap("BXRequestTracker").get("jdbcUrl").toString();
    private static Connection connection = null;
    private static Statement statement = null;

    public static void insertHostname(String hostname, String rid, String fleet, String locale)
    {
        locale.toUpperCase();
        String sql = "UPDATE " + locale + "REQUESTTRACKER SET " + fleet 
                + "='" + hostname + "' WHERE RID='" + rid + "'";
        try {
            statement.execute(sql);
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Statement connectToDatabase() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(DataBaseAccessUtils.jdbcUrl);
            statement = connection.createStatement();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return statement;
    }

此外,我发现当有单线程时错误没有出现当多个线程同时尝试更新数据库时,它会出现。

Also I have observed that the error does not come when there is a single threaded execution , it comes up when multiple threads are trying to update the database simultaneously.

推荐答案

为连接创建一个Utility类管理层在整个应用程序中单点管理它。

不要加载 DataSource 每次需要新连接时。

Don't load the DataSource every time you need a new connection.

示例代码:

public class ConnectionUtil {

    private DataSource dataSource;

    private static ConnectionUtil instance = new ConnectionUtil();

    private ConnectionUtil() {
        try {
            Context initContext = new InitialContext();
            dataSource = (DataSource) initContext.lookup("JNDI_LOOKUP_NAME");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

    public static ConnectionUtil getInstance() {
        return instance;
    }

    public Connection getConnection() throws SQLException {
        Connection connection = dataSource.getConnection();
        return connection;
    }

    public void close(Connection connection) throws SQLException {
        if (connection != null && !connection.isClosed()) {
            connection.close();
        }
        connection = null;
    }

}

始终关闭连接并在 try-catch-finally

Always close the connection and handle it in try-catch-finally

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = ConnectionUtil.getInstance().getConnection();

            ...
        } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                ConnectionUtil.getInstance().close(conn);
            }
        }

这篇关于声明结束后不允许任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆