优化jdbc executor,增加日志打印

This commit is contained in:
zhouhao
2017-07-19 16:27:20 +08:00
parent 216ee6a328
commit cf8108eaaa
3 changed files with 28 additions and 3 deletions

View File

@@ -16,14 +16,23 @@ import java.util.Objects;
*/
@Transactional(rollbackFor = Throwable.class)
public class DefaultJdbcExecutor extends AbstractJdbcSqlExecutor {
@Override
public Connection getConnection() {
return DataSourceUtils.getConnection(DataSourceHolder.currentDataSource().getNative());
DataSource dataSource = DataSourceHolder.currentDataSource().getNative();
Connection connection = DataSourceUtils.getConnection(dataSource);
boolean isConnectionTransactional = DataSourceUtils.isConnectionTransactional(connection, dataSource);
if (logger.isDebugEnabled()) {
logger.debug("DataSource ({}) JDBC Connection [{}] will {}be managed by Spring", DataSourceHolder.switcher().currentDataSourceId(), connection, (isConnectionTransactional ? "" : "not "));
}
return connection;
}
@Override
public void releaseConnection(Connection connection) throws SQLException {
if (logger.isDebugEnabled()) {
logger.debug("Releasing DataSource ({}) JDBC Connection [{}]", DataSourceHolder.switcher().currentDataSourceId(), connection);
}
DataSourceUtils.releaseConnection(connection, DataSourceHolder.currentDataSource().getNative());
}

View File

@@ -25,6 +25,12 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>

View File

@@ -7,6 +7,7 @@ import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@@ -20,11 +21,20 @@ import java.sql.SQLException;
public class JtaJdbcSqlExecutor extends AbstractJdbcSqlExecutor {
@Override
public Connection getConnection() {
return DataSourceUtils.getConnection(DataSourceHolder.currentDataSource().getNative());
DataSource dataSource = DataSourceHolder.currentDataSource().getNative();
Connection connection = DataSourceUtils.getConnection(dataSource);
boolean isConnectionTransactional = DataSourceUtils.isConnectionTransactional(connection, dataSource);
if (logger.isDebugEnabled()) {
logger.debug("DataSource ({}) JDBC Connection [{}] will {}be managed by Spring", DataSourceHolder.switcher().currentDataSourceId(), connection, (isConnectionTransactional ? "" : "not "));
}
return connection;
}
@Override
public void releaseConnection(Connection connection) throws SQLException {
if (logger.isDebugEnabled()) {
logger.debug("Releasing DataSource ({}) JDBC Connection [{}]", DataSourceHolder.switcher().currentDataSourceId(), connection);
}
DataSourceUtils.releaseConnection(connection, DataSourceHolder.currentDataSource().getNative());
}