Introduce rollbackBeforeClose property and AutoCloseable implementation

Closes gh-27249
This commit is contained in:
Juergen Hoeller
2023-11-30 16:36:32 +01:00
parent c56c304536
commit 5f9702b2a4
2 changed files with 166 additions and 16 deletions

View File

@@ -0,0 +1,100 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.jdbc.datasource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
/**
* @author Juergen Hoeller
* @since 6.1.2
*/
public class SingleConnectionDataSourceTests {
private final Connection connection = mock();
@Test
public void plainConnection() throws Exception {
SingleConnectionDataSource ds = new SingleConnectionDataSource(connection, false);
ds.getConnection().close();
verify(connection, times(1)).close();
}
@Test
public void withAutoCloseable() throws Exception {
try (SingleConnectionDataSource ds = new SingleConnectionDataSource(connection, false)) {
ds.getConnection();
}
verify(connection, times(1)).close();
}
@Test
public void withSuppressClose() throws Exception {
SingleConnectionDataSource ds = new SingleConnectionDataSource(connection, true);
ds.getConnection().close();
verify(connection, never()).close();
ds.destroy();
verify(connection, times(1)).close();
given(connection.isClosed()).willReturn(true);
assertThatExceptionOfType(SQLException.class).isThrownBy(ds::getConnection);
}
@Test
public void withRollbackBeforeClose() throws Exception {
SingleConnectionDataSource ds = new SingleConnectionDataSource(connection, true);
ds.setRollbackBeforeClose(true);
ds.destroy();
verify(connection, times(1)).rollback();
verify(connection, times(1)).close();
}
@Test
public void withEnforcedAutoCommit() throws Exception {
SingleConnectionDataSource ds = new SingleConnectionDataSource() {
@Override
protected Connection getConnectionFromDriverManager(String url, Properties props) {
return connection;
}
};
ds.setUrl("url");
ds.setAutoCommit(true);
ds.getConnection();
verify(connection, times(1)).setAutoCommit(true);
ds.destroy();
verify(connection, times(1)).close();
}
}