@Bean @Qualifier("Runelite Cache SQL2O") public Sql2o sql2o(DataSource dataSource) { Map<Class, Converter> converters = new HashMap<>(); converters.put(Instant.class, new InstantConverter()); return new Sql2o(dataSource, new NoQuirks(converters)); }
try (Connection con = sql2o.beginTransaction())
/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable * @param argument */ public void withConnection(StatementRunnable runnable, Object argument) { Connection connection = null; try{ connection = open(); runnable.run(connection, argument); } catch (Throwable t) { throw new Sql2oException("An error occurred while executing StatementRunnable", t); } finally{ if (connection != null) { connection.close(); } } }
/** * Begins a transaction with the given isolation level. Every statement executed on the return {@link Connection} * instance, will be executed in the transaction. It is very important to always call either the {@link org.sql2o.Connection#commit()} * method or the {@link org.sql2o.Connection#rollback()} method to close the transaction. Use proper try-catch logic. * @param isolationLevel the isolation level of the transaction * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(int isolationLevel){ return beginTransaction(getConnectionSource(), isolationLevel); }
private Query(Connection connection, String queryText, boolean returnGeneratedKeys, String[] columnNames) { this.connection = connection; this.returnGeneratedKeys = returnGeneratedKeys; this.columnNames = columnNames; this.setColumnMappings(connection.getSql2o().getDefaultColumnMappings()); this.caseSensitive = connection.getSql2o().isDefaultCaseSensitive(); paramNameToIdxMap = new HashMap<>(); parameters = new HashMap<>(); parsedQuery = connection.getSql2o().getQuirks().getSqlParameterParsingStrategy().parseSql(queryText, paramNameToIdxMap); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String DB_URL = "jdbc:mysql://localhost:3306/salesteam"; String USER = "root"; String PASS = "1234 "; Sql2o sql2o = new Sql2o(DB_URL, USER, PASS); String sql = "SELECT empID, fName " + "FROM salesteam " + "WHERE empID = 1"; List<Person> people; try (Connection con = sql2o.open()){ people = con.createQuery(sql).executeAndFetch(Person.class); } // code to generate json here... }
public Connection getConnection(boolean serialisable) { if (serialisable) { return database.beginTransaction(TRANSACTION_SERIALIZABLE); } else { return database.open(); } } }
/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable * @param <V> * @return */ public <V> V withConnection(StatementRunnableWithResult<V> runnable) { return withConnection(runnable, null); }
private Query(Connection connection, String queryText, boolean returnGeneratedKeys, String[] columnNames) { this.connection = connection; this.returnGeneratedKeys = returnGeneratedKeys; this.columnNames = columnNames; this.setColumnMappings(connection.getSql2o().getDefaultColumnMappings()); this.caseSensitive = connection.getSql2o().isDefaultCaseSensitive(); parsedQuery = queryText; }
Connection(Sql2o sql2o, ConnectionSource connectionSource, boolean autoClose) { this.connectionSource = connectionSource != null ? connectionSource : sql2o.getConnectionSource(); this.autoClose = autoClose; this.sql2o = sql2o; createConnection(); }
/** * Begins a transaction with the given isolation level. Every statement executed on the return {@link Connection} * instance, will be executed in the transaction. It is very important to always call either the {@link org.sql2o.Connection#commit()} * method or the {@link org.sql2o.Connection#rollback()} method to close the transaction. Use proper try-catch logic. * @param isolationLevel the isolation level of the transaction * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(int isolationLevel){ return beginTransaction(getConnectionSource(), isolationLevel); }
/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable */ public void withConnection(StatementRunnable runnable) { withConnection(runnable, null); }
private Query(Connection connection, String queryText, boolean returnGeneratedKeys, String[] columnNames) { this.connection = connection; this.returnGeneratedKeys = returnGeneratedKeys; this.columnNames = columnNames; this.setColumnMappings(connection.getSql2o().getDefaultColumnMappings()); this.caseSensitive = connection.getSql2o().isDefaultCaseSensitive(); paramNameToIdxMap = new HashMap<>(); parameters = new HashMap<>(); parsedQuery = connection.getSql2o().getQuirks().getSqlParameterParsingStrategy().parseSql(queryText, paramNameToIdxMap); }
Connection(Sql2o sql2o, ConnectionSource connectionSource, boolean autoClose) { this.connectionSource = connectionSource != null ? connectionSource : sql2o.getConnectionSource(); this.autoClose = autoClose; this.sql2o = sql2o; createConnection(); }
/** * Begins a transaction with isolation level {@link java.sql.Connection#TRANSACTION_READ_COMMITTED}. Every statement executed on the return {@link Connection} * instance, will be executed in the transaction. It is very important to always call either the {@link org.sql2o.Connection#commit()} * method or the {@link org.sql2o.Connection#rollback()} method to close the transaction. Use proper try-catch logic. * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(){ return this.beginTransaction(java.sql.Connection.TRANSACTION_READ_COMMITTED); }
/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable * @param argument * @param <V> * @return */ @SuppressWarnings("unchecked") public <V> V withConnection(StatementRunnableWithResult<V> runnable, Object argument) { Connection connection = null; try{ connection = open(); return (V)runnable.run(connection, argument); } catch (Throwable t) { throw new Sql2oException("An error occurred while executing StatementRunnable", t); } finally { if (connection != null) { connection.close(); } } }
@Override public Sql2o get() { return new Sql2o(dataSource, quirks); } }
/** * Begins a transaction with the given isolation level. Every statement executed on the return {@link Connection} * instance, will be executed in the transaction. It is very important to always call either the {@link Connection#commit()} * method or the {@link Connection#rollback()} method to close the transaction. Use proper try-catch logic. * * @param isolationLevel the isolation level of the transaction * @return the {@link Connection} instance to use to run statements in the transaction. */ public Connection beginTransaction(int isolationLevel) { return beginTransaction(getConnectionSource(), isolationLevel); }
/** * Invokes the run method on the {@link org.sql2o.StatementRunnableWithResult} instance. This method guarantees that * the connection is closed properly, when either the run method completes or if an exception occurs. * @param runnable */ public void withConnection(StatementRunnable runnable) { withConnection(runnable, null); }
Connection(Sql2o sql2o, ConnectionSource connectionSource, boolean autoClose) { this.connectionSource = connectionSource != null ? connectionSource : sql2o.getConnectionSource(); this.autoClose = autoClose; this.sql2o = sql2o; createConnection(); }