SQLITE_BUSY 数据库被锁定java [英] SQLITE_BUSY the database is locked java

查看:42
本文介绍了SQLITE_BUSY 数据库被锁定java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个使用 sqlite 数据库的 java 软件.整个数据库运行顺利,但是在运行应用程序一段时间后,我收到以下消息(来自 try catch 块):

I have create a java software which make use of sqlite database. The whole database works smoothly however after some time of running the application I am reveiving the following message (from a try catch block):

java.sql.SQLException: [SQLITE_BUSY] 数据库文件被锁定(数据库被锁定)

java.sql.SQLException: [SQLITE_BUSY] The database file is locked (database is locked)

每次出现异常时,我都通过关闭软件解决了我的问题.但是,有没有办法关闭我的数据库,以免每次都关闭软件?

I ve solved my problems by closing the software every time the exception rising. However is there a way to close my database instead so as not to close the software every time?

我有很多疑问,但我的问题总是出现在特定点:

I ve got plenty of queries however my prob arises always in a specific point:

        try {
            String query = "select * from StudentsSession where userId=? and course=? and level=?";
            PreparedStatement pst = connectionUsers.prepareStatement(query);
            pst.setString(1, saveUser.getText());
            pst.setString(2, textSubjectQTest);
            st.setString(3, showCurrentLevelLabel.getText());
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                count = count + 1;
            }
            pst.close();
            rs.close();

        } catch (Exception a) {
            System.out.println(a);
        }
        try {
            String countS, tmpS;
            countS = String.valueOf(count);
            sessionId.setText(countS);
            long unixTime = System.currentTimeMillis() / 1000L;
            tmpS = String.valueOf(unixTime);
            date.setText(tmpS);
            course.setText(textSubjectQTest);

            String query = "insert into StudentsSession (userId,date,course,level,trial,score) values (?,?,?,?,?,-1)";
            PreparedStatement pst = connectionUsers.prepareStatement(query);
            pst.setString(1, saveUser.getText());
            pst.setString(2, tmpS);
            pst.setString(3, textSubjectQTest);
            pst.setString(4, showCurrentLevelLabel.getText());
            pst.setString(5, countS);
            pst.executeUpdate();
            pst.close();

        } catch (Exception a) {

            System.out.println(a);
            System.exit(0);
        }

        String file1 = "";
        ResultSet ts4;
        try {

            sessionId3 = "";
            String query3 = "select * from studentssession where userid = ?  and course = ? and level = ?";
            PreparedStatement pst__1 = connectionUsers.prepareStatement(query3);
            pst__1.setString(1, saveUser.getText());
            pst__1.setString(2, textSubjectQTest);
            pst__1.setString(3, showCurrentLevelLabel.getText());
            ts4 = pst__1.executeQuery();

            while (ts4.next()) {
                sessionId3 = ts4.getString("sessionId");
            }
            pst__1.close();
            ts4.close();
            obj = new CaptureVideoFromWebCamera();

            file1 = "videos/" + userTextFieldS.getText();

            file1 = file1 + "_" + sessionId3;
            file1 = file1 + ".wmv";

            obj.start(file1);

        } catch (Exception e4) {
            e4.getCause();
        }

有时此代码会引发异常.

Sometimes this code rise the exception.

推荐答案

你的 try catch 错了,你尝试关闭 try 块中的 ResultSetStatemnt一个 finally 块.这可能会导致泄漏.

Your try catch are wrong, you try to close the ResultSet and Statemnt in the try block instead of a finally block. This could lead to leaks.

你应该这样做,最后.

PreparedStatement pst  = null;
ResultSet rs = null;
try {
        pst = connectionUsers.prepareStatement(query);
        ...  
        rs = pst.executeQuery();
        ...                    
    } catch (Exception a) {
        a.printStackTrace();
    } finally {
        if(rs != null){
             try{
                  rs.close();
             } catch(Exception e){
                 e.printStackTrace();
             }
        }
        if(pst != null){
            try{
                pst.close();
            } catch(Exception e){
                e.printStackTrace();
            }
        }
    }

或者您可以查看 Try-with-resource.

Or you could look to the Try-with-resource.

这可能是一个原因.

这篇关于SQLITE_BUSY 数据库被锁定java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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