Search code examples
javasqlitedb-browser-sqlite

SQLITE_BUSY The database file is locked


I'm getting the error [SQLITE_BUSY] The database file is locked (database is locked), even though I close my connection and the statements. Not sure what else to try.

 public void getTeams() throws SQLException {
    Connection c = DriverManager.getConnection("jdbc:sqlite:leagueDatabase.db");
    Statement teamStmt = c.createStatement();
    ResultSet teamData = teamStmt.executeQuery("SELECT * FROM TEAMS");

    while (teamData.next()) {
        teams.add(new Team(teamData.getString("city"), teamData.getString("name")));
    }
    teamData.close();
    teamStmt.close();
    c.close();

    Connection c2 = DriverManager.getConnection("jdbc:sqlite:leagueDatabase.db");
    Statement teamStatsStmt = c2.createStatement();
    ResultSet teamStatsData = teamStatsStmt.executeQuery("SELECT * FROM TEAM_STATS");

    for (int i = 0; i < teams.size(); i++) {
        for (int j = 0; j < players.size(); j++) {
            if (players.get(j).getTeam().equalsIgnoreCase(teams.get(i).getName()))
                teams.get(i).addPlayer(players.get(j));
        }
        teams.get(i).setStats(new TeamStats(teamStatsData.getInt("wins"), teamStatsData.getInt("losses"), (int) Math.round(teams.get(i).calcTeamRating()), SEASON, teams.get(i).getName()));
    }
}

Solution

  • You are closing the first connection (c) - but not the second connection (c2).

    Is there a reason you're creating two connections? Why not simply create both statements on the first connection, then close the first connection (c) after the second statement? Then you could avoid the second connection entirely (and the fact that you're not closing it is likely to be the cause of your error)