CrispDbKit相关代码结构调整.
This commit is contained in:
+6
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
+32
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
+19
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user