Merge pull request #2352 from mposolda/master

KEYCLOAK-2601 Fix incorrectly autodetected dialect with MSSQL2014
This commit is contained in:
Stian Thorgersen 2016-03-10 07:29:28 +01:00
commit 6e0eb21428
3 changed files with 56 additions and 9 deletions

View file

@ -220,8 +220,8 @@ bin/add-user-keycloak.[sh|bat] -r master -u <username> -p <password>
<title>Tested databases</title> <title>Tested databases</title>
<para> <para>
Here is list of RDBMS databases and corresponding JDBC drivers, which were tested with Keycloak. Note that Hibernate dialect Here is list of RDBMS databases and corresponding JDBC drivers, which were tested with Keycloak. Note that Hibernate dialect
is usually set automatically according to your database, but in some cases, you must manually set the proper dialect, is usually set automatically according to your database, but you have possibility to override if default dialect doesn't work correctly.
as the default dialect may not work correctly. You can setup dialect by adding property <literal>driverDialect</literal> You can setup dialect by adding property <literal>driverDialect</literal>
to the <literal>keycloak-server.json</literal> into <literal>connectionsJpa</literal> section (see above). to the <literal>keycloak-server.json</literal> into <literal>connectionsJpa</literal> section (see above).
<table frame='all'><title>Tested databases</title> <table frame='all'><title>Tested databases</title>
<tgroup cols='3' align='left' colsep='1' rowsep='1'> <tgroup cols='3' align='left' colsep='1' rowsep='1'>
@ -261,7 +261,7 @@ bin/add-user-keycloak.[sh|bat] -r master -u <username> -p <password>
<row> <row>
<entry>Microsoft SQL Server 2012</entry> <entry>Microsoft SQL Server 2012</entry>
<entry>Microsoft SQL Server JDBC Driver 4.0.2206.100</entry> <entry>Microsoft SQL Server JDBC Driver 4.0.2206.100</entry>
<entry>org.hibernate.dialect.SQLServer2008Dialect</entry> <entry>auto</entry>
</row> </row>
<row> <row>
<entry>IBM DB2 10.5</entry> <entry>IBM DB2 10.5</entry>

View file

@ -121,11 +121,6 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide
} }
} }
String driverDialect = config.get("driverDialect");
if (driverDialect != null && driverDialect.length() > 0) {
properties.put("hibernate.dialect", driverDialect);
}
String schema = getSchema(); String schema = getSchema();
if (schema != null) { if (schema != null) {
properties.put(JpaUtils.HIBERNATE_DEFAULT_SCHEMA, schema); properties.put(JpaUtils.HIBERNATE_DEFAULT_SCHEMA, schema);
@ -147,6 +142,11 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide
connection = getConnection(); connection = getConnection();
try{ try{
prepareOperationalInfo(connection); prepareOperationalInfo(connection);
String driverDialect = detectDialect(connection);
if (driverDialect != null) {
properties.put("hibernate.dialect", driverDialect);
}
if (databaseSchema != null) { if (databaseSchema != null) {
logger.trace("Updating database"); logger.trace("Updating database");
@ -209,7 +209,7 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide
} }
} }
} }
protected void prepareOperationalInfo(Connection connection) { protected void prepareOperationalInfo(Connection connection) {
try { try {
operationalInfo = new LinkedHashMap<>(); operationalInfo = new LinkedHashMap<>();
@ -225,6 +225,42 @@ public class DefaultJpaConnectionProviderFactory implements JpaConnectionProvide
} }
} }
protected String detectDialect(Connection connection) {
String driverDialect = config.get("driverDialect");
if (driverDialect != null && driverDialect.length() > 0) {
return driverDialect;
} else {
try {
String dbProductName = connection.getMetaData().getDatabaseProductName();
String dbProductVersion = connection.getMetaData().getDatabaseProductVersion();
// For MSSQL2014, we may need to fix the autodetected dialect by hibernate
if (dbProductName.equals("Microsoft SQL Server")) {
String topVersionStr = dbProductVersion.split("\\.")[0];
boolean shouldSet2012Dialect = true;
try {
int topVersion = Integer.parseInt(topVersionStr);
if (topVersion < 12) {
shouldSet2012Dialect = false;
}
} catch (NumberFormatException nfe) {
}
if (shouldSet2012Dialect) {
String sql2012Dialect = "org.hibernate.dialect.SQLServer2012Dialect";
logger.debugf("Manually override hibernate dialect to %s", sql2012Dialect);
return sql2012Dialect;
}
}
} catch (SQLException e) {
logger.warnf("Unable to detect hibernate dialect due database exception : %s", e.getMessage());
}
return null;
}
}
@Override @Override
public Connection getConnection() { public Connection getConnection() {
try { try {

View file

@ -211,6 +211,17 @@ public class UserModelTest extends AbstractModelTest {
Assert.assertEquals("val23", attrVals.get(0)); Assert.assertEquals("val23", attrVals.get(0));
} }
@Test
public void testSearchByString() {
RealmModel realm = realmManager.createRealm("original");
UserModel user1 = session.users().addUser(realm, "user1");
commit();
List<UserModel> users = session.users().searchForUser("user", realm, 0, 7);
Assert.assertTrue(users.contains(user1));
}
@Test @Test
public void testSearchByUserAttribute() throws Exception { public void testSearchByUserAttribute() throws Exception {
RealmModel realm = realmManager.createRealm("original"); RealmModel realm = realmManager.createRealm("original");