private String getInvoiceId() {

        String data = null;
        try {
            Connection conn = getConnection();
            boolean error = true;

            conn.setAutoCommit(false);

            conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);

            while (error) {
                try {
                    PreparedStatement ps1 = conn.prepareStatement("select min(invoice_id) from invoicetable where isused=0");
                    ResultSet rs1 = ps1.executeQuery();
                    if(rs1.next()) {
                        data = rs1.getString(1);
                    }
                    PreparedStatement psUpdate = conn.prepareStatement("update invoicetable set isused=1 where invoice_id=?");
                    psUpdate.setString(1, data);
                    psUpdate.executeUpdate();
                    
                    conn.commit();
                    
                    rs1.close();
                    ps1.close();
                    psUpdate.close();
                    
                    
                    error = false;
                } catch (Exception err) {
                    conn.rollback();
                    Thread.sleep(random.nextInt(50));
                }
            }
            conn.close();
        } catch (Exception err) {
            err.printStackTrace();
        }
        return data;
    }