All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.neo4j.jdbc.bolt.BoltConnection Maven / Gradle / Ivy

There is a newer version: 5.0.0
Show newest version
/*
 * Copyright (c) 2016 LARUS Business Automation [http://www.larus-ba.it]
 * 

* This file is part of the "LARUS Integration Framework for Neo4j". *

* The "LARUS Integration Framework for Neo4j" is licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *

* Created on 17/02/16 */ package org.neo4j.jdbc.bolt; import org.neo4j.driver.v1.Session; import org.neo4j.driver.v1.Transaction; import org.neo4j.jdbc.*; import org.neo4j.jdbc.utils.UncaughtExceptionLogger; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ArrayList; import java.util.Properties; /** * @author AgileLARUS * @since 3.0.0 */ public class BoltConnection extends Connection implements Loggable { private Session session; private Transaction transaction; private boolean autoCommit = true; private boolean loggable = false; /** * Constructor with Session and Properties. * * @param session Bolt Session * @param properties Driver properties * @param url Url used for this connection */ public BoltConnection(Session session, Properties properties, String url) { super(properties, url, BoltResultSet.DEFAULT_HOLDABILITY); this.session = session; } /** * Constructor with Session. * * @param session Bolt Session */ public BoltConnection(Session session) { this(session, new Properties(), ""); } /** * Getter for transaction. * * @return */ public Transaction getTransaction() { return this.transaction; } /** * Getter for session. * * @return */ public Session getSession() { return this.session; } @Override public DatabaseMetaData getMetaData() throws SQLException { return new BoltDatabaseMetaData(this); } /*------------------------------*/ /* Commit, rollback */ /*------------------------------*/ @Override public void setAutoCommit(boolean autoCommit) throws SQLException { if (this.autoCommit != autoCommit) { if (this.transaction != null && !this.autoCommit) { this.commit(); this.transaction.close(); } if (this.autoCommit) { //Simply restart the transaction this.transaction = this.session.beginTransaction(); } this.autoCommit = autoCommit; } } @Override public boolean getAutoCommit() throws SQLException { this.checkClosed(); return autoCommit; } @Override public void commit() throws SQLException { this.checkClosed(); this.checkAutoCommit(); if (this.transaction == null) { throw new SQLException("The transaction is null"); } this.transaction.success(); this.transaction.close(); this.transaction = this.session.beginTransaction(); } @Override public void rollback() throws SQLException { this.checkClosed(); this.checkAutoCommit(); if (this.transaction == null) { throw new SQLException("The transaction is null"); } this.transaction.failure(); } /*------------------------------*/ /* Create Statement */ /*------------------------------*/ @Override public Statement createStatement() throws SQLException { this.checkClosed(); if (this.transaction == null && !this.autoCommit) { this.transaction = this.session.beginTransaction(); } return InstanceFactory .debug(BoltStatement.class, new BoltStatement(this, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT), this.isLoggable()); } @Override public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { this.checkClosed(); this.checkTypeParams(resultSetType); this.checkConcurrencyParams(resultSetConcurrency); return InstanceFactory .debug(BoltStatement.class, new BoltStatement(this, resultSetType, resultSetConcurrency, ResultSet.CLOSE_CURSORS_AT_COMMIT), this.isLoggable()); } @Override public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { this.checkClosed(); this.checkTypeParams(resultSetType); this.checkConcurrencyParams(resultSetConcurrency); this.checkHoldabilityParams(resultSetHoldability); return InstanceFactory .debug(BoltStatement.class, new BoltStatement(this, resultSetType, resultSetConcurrency, resultSetHoldability), this.isLoggable()); } /*-------------------------------*/ /* Prepare Statement */ /*-------------------------------*/ @Override public PreparedStatement prepareStatement(String sql) throws SQLException { this.checkClosed(); return InstanceFactory.debug(BoltPreparedStatement.class, new BoltPreparedStatement(this, nativeSQL(sql), ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT), this.isLoggable()); } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { this.checkClosed(); this.checkTypeParams(resultSetType); this.checkConcurrencyParams(resultSetConcurrency); return InstanceFactory.debug(BoltPreparedStatement.class, new BoltPreparedStatement(this, sql, resultSetType, resultSetConcurrency, ResultSet.CLOSE_CURSORS_AT_COMMIT), this.isLoggable()); } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { this.checkClosed(); this.checkTypeParams(resultSetType); this.checkConcurrencyParams(resultSetConcurrency); this.checkHoldabilityParams(resultSetHoldability); return InstanceFactory .debug(BoltPreparedStatement.class, new BoltPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability), this.isLoggable()); } /*-------------------*/ /* Close */ /*-------------------*/ @Override public boolean isClosed() throws SQLException { return !this.session.isOpen(); } @Override public void close() throws SQLException { try { if (!this.isClosed()) { session.close(); } } catch (Exception e) { throw new SQLException("A database access error has occurred"); } } /*-------------------*/ /* isValid */ /*-------------------*/ @Override public boolean isValid(int timeout) throws SQLException { if (timeout < 0) { throw new SQLException("Timeout can't be less than zero"); } if (this.isClosed()) { return false; } UncaughtExceptionLogger h = new UncaughtExceptionLogger(); Thread t = new Thread() { public void run() { Session session = getSession(); Transaction transaction = getTransaction(); if (transaction != null && transaction.isOpen()) { transaction.run(FASTEST_STATEMENT); } else { session.run(FASTEST_STATEMENT); } } }; t.setUncaughtExceptionHandler(h); try { t.start(); t.join(timeout * 1000); } catch (InterruptedException e) { } if (t.isAlive()) { t.interrupt(); return false; } if (!h.getExceptions().isEmpty()) { return false; } return true; } /*--------------------*/ /* Logger */ /*--------------------*/ @Override public boolean isLoggable() { return this.loggable; } @Override public void setLoggable(boolean loggable) { this.loggable = loggable; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy