title: Java - SQLite database locked
body: <p>I'm heaving a problem with SQLite and Java.</p>
  
  <p>I don't know if it's because I'm leaving some open connection (or PreparedStatement/ResultSet) or if it's because I'm handling the Connection in a wrong way but this code is giving me a hard time for the past 2 days.</p>
  
  <p>Im trying to get all clientes registered with the getClientes() method, thats when I got the "database file locked" error.</p>
  
  <p>I've created the above class to create the connection to the SQLite DB:</p>
  
  <pre><code>public class ConnectionFactory
  {
      public Connection getConnection()
      {
          Connection c = null;
  
          try
          {
              System.out.println("Conectando ao banco de dados...");
              Class.forName("org.sqlite.JDBC");
              c = DriverManager.getConnection("jdbc:sqlite:src/lp2/database/rivieraresort.db");
              System.out.println("Conectado com sucesso!");
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
  
          return c;
      }
  }
  </code></pre>
  
  <p>And the following one to handle the DB itself</p>
  
  <pre><code>public class RivieraDB
  {   
      public Quarto buscarQuarto(int contratoID) throws Exception
      {
          Quarto quartoEncontrado = null;
  
          Connection c = new ConnectionFactory().getConnection();
          c = new ConnectionFactory().getConnection();
          String query = "SELECT * FROM quartos WHERE contrato_id = ?";
          PreparedStatement pstmt = c.prepareStatement(query);
  
          pstmt.setInt(1, contratoID);
  
          ResultSet rs = pstmt.executeQuery();
  
          while(rs.next())
          {
              String tipo = rs.getString("tipo");
              int camasExtras = rs.getInt("camas_extras");
              double preco = rs.getDouble("preco");
  
              quartoEncontrado = new Quarto(tipo, camasExtras, preco);
          }
  
          rs.close();
          pstmt.close();
          c.close();
          return quartoEncontrado;
      }
  
      public Contrato buscarContrato(int contratoID) throws Exception
      {
          Contrato contratoEncontrado = null;
  
          Connection c = new ConnectionFactory().getConnection();
          Quarto quartoAssociado = buscarQuarto(contratoID);
          c = new ConnectionFactory().getConnection();
          String query = "SELECT * FROM contratos WHERE id = ?";
          PreparedStatement pstmt = c.prepareStatement(query);
  
          pstmt.setInt(1, contratoID);
  
          ResultSet rs = pstmt.executeQuery();
  
          while(rs.next())
          {
              String inicioContrato = rs.getString("data_inicio");
              int tempo = rs.getInt("tempo");
  
              contratoEncontrado = new Contrato(quartoAssociado, inicioContrato, tempo);
          }
  
          rs.close();
          pstmt.close();
          c.close();
          return contratoEncontrado;
      }
  
      public ArrayList&lt;Cliente&gt; getClientes() throws Exception
      {
          ArrayList&lt;Cliente&gt; clientesEncontrados = new ArrayList&lt;Cliente&gt;();
          Cliente clienteEncontrado = null;
          Connection c = new ConnectionFactory().getConnection();
          String query = "SELECT * FROM clientes";
          PreparedStatement pstmt = c.prepareStatement(query);
  
          ResultSet rs = pstmt.executeQuery();
  
          while(rs.next())
          {
              String nome = rs.getString("nome");
              String cpf = rs.getString("cpf");
              String nascimento = rs.getString("nascimento");
              String endereco = rs.getString("endereco");
              String cartao = rs.getString("cartao");
              int contratoID = rs.getInt("contrato_id");
              boolean statusHospedagem = rs.getBoolean("status_hospedagem");
  
              clienteEncontrado = new Cliente(nome, cpf, nascimento, endereco);
              clienteEncontrado.setCartao(cartao);
              clienteEncontrado.setContrato(buscarContrato(contratoID));
              clienteEncontrado.setEstaHospedado(statusHospedagem);
  
              clientesEncontrados.add(clienteEncontrado);
          }
  
          rs.close();
          pstmt.close();
          c.close();
          return clientesEncontrados;
      }
  }
  </code></pre>
  
  <p>The "[SQLITE_BUSY]  The database file is locked (database is locked)" error only occurs when I try to use the getClientes() method.</p>
  
  <p>Any ideas?</p>