com.jn.sqlhelper.common.transaction.jdbc.ConnectionTransactionalResource Maven / Gradle / Ivy
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache, 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.gnu.org/licenses/lgpl-2.0.html
*
* 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.
*/
package com.jn.sqlhelper.common.transaction.jdbc;
import com.jn.sqlhelper.common.transaction.TransactionalResource;
import java.sql.Connection;
import java.sql.SQLException;
public class ConnectionTransactionalResource implements TransactionalResource {
private Connection connection;
private String name = "undefined";
public ConnectionTransactionalResource(Connection connection) {
setConnection(connection);
}
public Connection getConnection() {
return connection;
}
public void setConnection(Connection connection) {
this.connection = connection;
}
@Override
public void commit(boolean force) throws SQLException {
if (this.connection != null && !this.connection.isClosed() && force) {
this.connection.commit();
}
}
@Override
public void rollback() throws SQLException {
if (this.connection != null && !this.connection.isClosed()) {
this.connection.rollback();
}
}
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return this.getName();
}
@Override
public boolean isClosed() {
try {
return connection == null || connection.isClosed();
} catch (Throwable ex) {
return false;
}
}
@Override
public void close() throws SQLException {
if (this.connection != null && !this.connection.isClosed()) {
this.connection.close();
}
}
}