From f378f9e7447b1818273a315116541a6571c2c7bf Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Mon, 7 Nov 2022 15:59:28 +0800 Subject: [PATCH] =?UTF-8?q?CrispDbKit=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../db/crisp/DefaultSqlSessionFactory.java | 6 + .../neutrino/core/db/crisp/JdbcException.java | 18 +++ .../db/crisp/SqlSessionFactoryBuilder.java | 32 +++++ .../core/db/crisp/tx/JdbcTransaction.java | 124 ++++++++++++++++++ .../core/db/crisp/tx/Transaction.java | 63 +++++++++ .../db/crisp/tx/TransactionException.java | 19 +++ 6 files changed, 262 insertions(+) create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/JdbcException.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlSessionFactoryBuilder.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/tx/JdbcTransaction.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/tx/Transaction.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/tx/TransactionException.java diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DefaultSqlSessionFactory.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DefaultSqlSessionFactory.java index e57ba3b2..19eb347c 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DefaultSqlSessionFactory.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/DefaultSqlSessionFactory.java @@ -29,6 +29,12 @@ import java.sql.Connection; */ public class DefaultSqlSessionFactory implements SqlSessionFactory { + private final DbConfig config; + + public DefaultSqlSessionFactory(DbConfig config) { + this.config = config; + } + @Override public SqlSession openSession() { // TODO diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/JdbcException.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/JdbcException.java new file mode 100644 index 00000000..664e9b98 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/JdbcException.java @@ -0,0 +1,18 @@ +package fun.asgc.neutrino.core.db.crisp; + +import fun.asgc.neutrino.core.exception.InternalException; + +/** + * @author: aoshiguchen + * @date: 2022/11/7 + */ +public class JdbcException extends InternalException { + + public JdbcException(String message) { + super(message); + } + + public JdbcException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlSessionFactoryBuilder.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlSessionFactoryBuilder.java new file mode 100644 index 00000000..ccd377e5 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/SqlSessionFactoryBuilder.java @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.db.crisp; + +/** + * @author: aoshiguchen + * @date: 2022/11/7 + */ +public class SqlSessionFactoryBuilder { + public SqlSessionFactory build(DbConfig config) { + return new DefaultSqlSessionFactory(config); + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/tx/JdbcTransaction.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/tx/JdbcTransaction.java new file mode 100644 index 00000000..98048119 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/tx/JdbcTransaction.java @@ -0,0 +1,124 @@ +package fun.asgc.neutrino.core.db.crisp.tx; + +import fun.asgc.neutrino.core.db.crisp.TransactionIsolationLevel; +import lombok.extern.slf4j.Slf4j; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; + +/** + * @author: aoshiguchen + * @date: 2022/11/7 + */ +@Slf4j +public class JdbcTransaction implements Transaction { + + protected Connection connection; + protected DataSource dataSource; + protected TransactionIsolationLevel level; + protected boolean autoCommit; + + public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) { + dataSource = ds; + level = desiredLevel; + autoCommit = desiredAutoCommit; + } + + public JdbcTransaction(Connection connection) { + this.connection = connection; + } + + @Override + public Connection getConnection() throws SQLException { + if (connection == null) { + openConnection(); + } + return connection; + } + + @Override + public void commit() throws SQLException { + if (connection != null && !connection.getAutoCommit()) { + if (log.isDebugEnabled()) { + log.debug("Committing JDBC Connection [" + connection + "]"); + } + connection.commit(); + } + } + + @Override + public void rollback() throws SQLException { + if (connection != null && !connection.getAutoCommit()) { + if (log.isDebugEnabled()) { + log.debug("Rolling back JDBC Connection [" + connection + "]"); + } + connection.rollback(); + } + } + + @Override + public void close() throws SQLException { + if (connection != null) { + resetAutoCommit(); + if (log.isDebugEnabled()) { + log.debug("Closing JDBC Connection [" + connection + "]"); + } + connection.close(); + } + } + + @Override + public Integer getTimeout() throws SQLException { + return null; + } + + protected void openConnection() throws SQLException { + if (log.isDebugEnabled()) { + log.debug("Opening JDBC Connection"); + } + connection = dataSource.getConnection(); + if (level != null) { + connection.setTransactionIsolation(level.getLevel()); + } + setDesiredAutoCommit(autoCommit); + } + + protected void setDesiredAutoCommit(boolean desiredAutoCommit) { + try { + if (connection.getAutoCommit() != desiredAutoCommit) { + if (log.isDebugEnabled()) { + log.debug("Setting autocommit to " + desiredAutoCommit + " on JDBC Connection [" + connection + "]"); + } + connection.setAutoCommit(desiredAutoCommit); + } + } catch (SQLException e) { + // Only a very poorly implemented driver would fail here, + // and there's not much we can do about that. + throw new TransactionException("Error configuring AutoCommit. " + + "Your driver may not support getAutoCommit() or setAutoCommit(). " + + "Requested setting: " + desiredAutoCommit + ". Cause: " + e, e); + } + } + + protected void resetAutoCommit() { + try { + if (!connection.getAutoCommit()) { + // MyBatis does not call commit/rollback on a connection if just selects were performed. + // Some databases start transactions with select statements + // and they mandate a commit/rollback before closing the connection. + // A workaround is setting the autocommit to true before closing the connection. + // Sybase throws an exception here. + if (log.isDebugEnabled()) { + log.debug("Resetting autocommit to true on JDBC Connection [" + connection + "]"); + } + connection.setAutoCommit(true); + } + } catch (SQLException e) { + if (log.isDebugEnabled()) { + log.debug("Error resetting autocommit to true " + + "before closing the connection. Cause: " + e); + } + } + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/tx/Transaction.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/tx/Transaction.java new file mode 100644 index 00000000..65baaf8e --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/tx/Transaction.java @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.db.crisp.tx; + +import java.sql.Connection; +import java.sql.SQLException; + +/** + * @author: aoshiguchen + * @date: 2022/11/7 + */ +public interface Transaction { + + /** + * Retrieve inner database connection. + * @return DataBase connection + * @throws SQLException + */ + Connection getConnection() throws SQLException; + + /** + * Commit inner database connection. + * @throws SQLException + */ + void commit() throws SQLException; + + /** + * Rollback inner database connection. + * @throws SQLException + */ + void rollback() throws SQLException; + + /** + * Close inner database connection. + * @throws SQLException + */ + void close() throws SQLException; + + /** + * Get transaction timeout if set. + * @throws SQLException + */ + Integer getTimeout() throws SQLException; +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/tx/TransactionException.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/tx/TransactionException.java new file mode 100644 index 00000000..853fc704 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/db/crisp/tx/TransactionException.java @@ -0,0 +1,19 @@ +package fun.asgc.neutrino.core.db.crisp.tx; + +import fun.asgc.neutrino.core.db.crisp.JdbcException; + +/** + * @author: aoshiguchen + * @date: 2022/11/7 + */ +public class TransactionException extends JdbcException { + + public TransactionException(String message) { + super(message); + } + + public TransactionException(String message, Throwable cause) { + super(message, cause); + } + +}