Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2012-2023, jcabi.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met: 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer. 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution. 3) Neither the name of the jcabi.com nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jcabi.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
import javax.sql.DataSource;
import lombok.EqualsAndHashCode;
import lombok.ToString;
/**
* Universal JDBC wrapper.
*
*
Execute a simple SQL query over a JDBC data source:
*
*
String name = new JdbcSession(source)
* .sql("SELECT name FROM foo WHERE id = ?")
* .set(123)
* .select(
* new Outcome<String>() {
* @Override
* public String handle(final ResultSet rset) throws SQLException {
* rset.next();
* return rset.getString(1);
* }
* }
* );
*
*
There are a number of convenient pre-defined outcomes, like
* {@link Outcome#VOID}, {@link Outcome#NOT_EMPTY}, {@link Outcome#UPDATE_COUNT}
* {@link SingleOutcome}, etc.
*
*
Methods {@link #insert(Outcome)},
* {@link #update(Outcome)},
* {@link #execute()}, and
* {@link #select(Outcome)} clean the list of arguments pre-set by
* {@link #set(Object)}. The class can be used for a complex transaction, when
* it's necessary to perform a number of SQL statements in a group. For
* example, the following construct will execute two SQL queries, in a single
* transaction and will "commit" at the end (or rollback the entire transaction
* in case of any error in between):
*
*
new JdbcSession(source)
* .autocommit(false)
* .sql("START TRANSACTION")
* .execute()
* .sql("DELETE FROM foo WHERE id = ?")
* .set(444)
* .execute()
* .set(555)
* .execute()
* .commit();
*
*
The following SQL queries will be sent to the database:
*
*
START TRANSACTION;
* DELETE FROM foo WHERE id = 444;
* DELETE FROM foo WHERE id = 555;
* COMMIT;
*
*
{@link #autocommit(boolean)} (with {@code false} as an argument)
* can be used when it's necessary to execute
* a statement and leave the connection open. For example when shutting down
* the database through SQL:
*
*
new JdbcSession(/* H2 Database data source */)
* .autocommit(false)
* .sql("SHUTDOWN COMPACT")
* .execute();
*
*
IMPORTANT:
*
*
If you rely on one specific {@link Connection} instance, be careful if
* you are using it in more places, especially if more references of this class
* use it - one of those references might close the connection if you forget
* to call {@link JdbcSession#autocommit(boolean)} with {@code false} as an argument,
* for example:
*
*
Connection connection = [...];
* DataSource ds = new StaticSource(connection);
* new JdbcSession(ds)
* .sql("SQL STATEMENT")
* .execute();
* new JdbcSession(ds)
* .sql("SQL STATEMENT 2")
* .execute();
*
The above example will fail because the first JdbcSession closes
* the connection, and the next one tries to work with it closed. In order to
* not have this failure, the first session has to call
* {@link #autocommit(boolean)} with {@code false} as an argument, like this:
*
*
Connection connection = [...];
* DataSource ds = new StaticSource(connection);
* new JdbcSession(ds)
* .autocommit(false)
* .sql("SQL STATEMENT")
* .execute();
* new JdbcSession(ds)
* .sql("SQL STATEMENT 2")
* .execute();
*
*
This class is thread-safe.
*
* @since 0.1.8
* @todo #51:30min Refactor this class to avoid too much coupling.
* For instance, CRUD operations could be performed by another class.
* Don't forget to remove the suppressions that become obsolete afterwards.
*/
@ToString
@EqualsAndHashCode(of = { "source", "connection", "args", "auto", "query" })
@SuppressWarnings({ "PMD.TooManyMethods", "PMD.CloseResource" })
public final class JdbcSession {
/**
* JDBC DataSource to get connections from.
*/
private final transient DataSource source;
/**
* Arguments.
*
*
Every time this attribute is modified, we must synchronize, because
* a non-thread-safe {@link LinkedList} is assigned to it.