Skip to content

Commit 18bebc3

Browse files
committed
VendorDatabaseIdProvider should throw exception if DB error occurs
As reported in mybatis#3040, swallowing the SQLException could lead to unexpected statement resolution. Theoretically, this change might expose a problem in an existing solution that was previously hidden.
1 parent c754ff2 commit 18bebc3

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/main/java/org/apache/ibatis/mapping/VendorDatabaseIdProvider.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
import javax.sql.DataSource;
2323

24-
import org.apache.ibatis.logging.Log;
25-
import org.apache.ibatis.logging.LogFactory;
24+
import org.apache.ibatis.builder.BuilderException;
2625

2726
/**
2827
* Vendor DatabaseId provider.
@@ -44,10 +43,9 @@ public String getDatabaseId(DataSource dataSource) {
4443
}
4544
try {
4645
return getDatabaseName(dataSource);
47-
} catch (Exception e) {
48-
LogHolder.log.error("Could not get a databaseId from dataSource", e);
46+
} catch (SQLException e) {
47+
throw new BuilderException("Error occurred when getting DB product name.", e);
4948
}
50-
return null;
5149
}
5250

5351
@Override
@@ -70,8 +68,4 @@ private String getDatabaseProductName(DataSource dataSource) throws SQLException
7068
}
7169
}
7270

73-
private static class LogHolder {
74-
private static final Log log = LogFactory.getLog(VendorDatabaseIdProvider.class);
75-
}
76-
7771
}

src/test/java/org/apache/ibatis/mapping/VendorDatabaseIdProviderTest.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package org.apache.ibatis.mapping;
1818

1919
import static org.junit.jupiter.api.Assertions.*;
20-
import static org.mockito.Mockito.*;
20+
import static org.mockito.Mockito.mock;
21+
import static org.mockito.Mockito.when;
2122

2223
import java.sql.Connection;
2324
import java.sql.DatabaseMetaData;
@@ -26,6 +27,7 @@
2627

2728
import javax.sql.DataSource;
2829

30+
import org.apache.ibatis.builder.BuilderException;
2931
import org.junit.jupiter.api.Test;
3032

3133
class VendorDatabaseIdProviderTest {
@@ -77,7 +79,12 @@ void shouldNullBeReturnedOnDbError() throws Exception {
7779
VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
7880
Properties properties = new Properties();
7981
properties.put("Ewok DB", "ewok");
80-
assertNull(provider.getDatabaseId(dataSource));
82+
try {
83+
provider.getDatabaseId(dataSource);
84+
fail("Should BuilderException be thrown.");
85+
} catch (BuilderException e) {
86+
// pass
87+
}
8188
}
8289

8390
private DataSource mockDataSource() throws SQLException {

0 commit comments

Comments
 (0)