Skip to content

Commit

Permalink
VendorDatabaseIdProvider should throw exception if DB error occurs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
harawata committed Dec 14, 2023
1 parent c754ff2 commit 18bebc3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

import javax.sql.DataSource;

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.apache.ibatis.builder.BuilderException;

/**
* Vendor DatabaseId provider.
Expand All @@ -44,10 +43,9 @@ public String getDatabaseId(DataSource dataSource) {
}
try {
return getDatabaseName(dataSource);
} catch (Exception e) {
LogHolder.log.error("Could not get a databaseId from dataSource", e);
} catch (SQLException e) {
throw new BuilderException("Error occurred when getting DB product name.", e);
}
return null;
}

@Override
Expand All @@ -70,8 +68,4 @@ private String getDatabaseProductName(DataSource dataSource) throws SQLException
}
}

private static class LogHolder {
private static final Log log = LogFactory.getLog(VendorDatabaseIdProvider.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package org.apache.ibatis.mapping;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
Expand All @@ -26,6 +27,7 @@

import javax.sql.DataSource;

import org.apache.ibatis.builder.BuilderException;
import org.junit.jupiter.api.Test;

class VendorDatabaseIdProviderTest {
Expand Down Expand Up @@ -77,7 +79,12 @@ void shouldNullBeReturnedOnDbError() throws Exception {
VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
Properties properties = new Properties();
properties.put("Ewok DB", "ewok");
assertNull(provider.getDatabaseId(dataSource));
try {
provider.getDatabaseId(dataSource);
fail("Should BuilderException be thrown.");
} catch (BuilderException e) {
// pass
}
}

private DataSource mockDataSource() throws SQLException {
Expand Down

0 comments on commit 18bebc3

Please sign in to comment.