Skip to content

Commit

Permalink
chore: code refactoring to make extension compatible with Hibernate 6…
Browse files Browse the repository at this point in the history
….3x to 6.6.x
  • Loading branch information
filipelautert committed Oct 8, 2024
1 parent 157279e commit 481d2d1
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected void configureSources(MetadataSources sources) throws DatabaseExceptio
ServiceRegistry standardRegistry = configuration.getStandardServiceRegistryBuilder()
.applySettings(config.getProperties())
.addService(ConnectionProvider.class, new NoOpConnectionProvider())
.addService(MultiTenantConnectionProvider.class, new NoOpConnectionProvider())
.addService(MultiTenantConnectionProvider.class, new NoOpMultiTenantConnectionProvider())
.build();

config.buildSessionFactory(standardRegistry);
Expand All @@ -67,4 +67,4 @@ protected String getDefaultDatabaseProductName() {
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;
import org.hibernate.service.ServiceRegistry;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicReference;

/**
Expand Down Expand Up @@ -190,7 +187,7 @@ protected MetadataSources createMetadataSources() throws DatabaseException {
.applySetting(AvailableSettings.DIALECT, dialect)
.applySetting(HibernateDatabase.HIBERNATE_TEMP_USE_JDBC_METADATA_DEFAULTS, Boolean.FALSE.toString())
.addService(ConnectionProvider.class, new NoOpConnectionProvider())
.addService(MultiTenantConnectionProvider.class, new NoOpConnectionProvider())
.addService(MultiTenantConnectionProvider.class, new NoOpMultiTenantConnectionProvider())
.build();

return new MetadataSources(standardRegistry);
Expand Down Expand Up @@ -335,59 +332,4 @@ public boolean supportsCatalogs() {
return true;
}

/**
* Used by hibernate to ensure no database access is performed.
*/
static class NoOpConnectionProvider implements ConnectionProvider, MultiTenantConnectionProvider {

@Override
public Connection getConnection() throws SQLException {
throw new SQLException("No connection");
}

@Override
public void closeConnection(Connection conn) throws SQLException {

}

@Override
public boolean supportsAggressiveRelease() {
return false;
}

@Override
public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) {
return ConnectionProvider.super.getDatabaseConnectionInfo(dialect);
}

@Override
public boolean isUnwrappableAs(Class unwrapType) {
return false;
}

@Override
public <T> T unwrap(Class<T> unwrapType) {
return null;
}

@Override
public Connection getAnyConnection() throws SQLException {
return getConnection();
}

@Override
public void releaseAnyConnection(Connection connection) throws SQLException {

}

@Override
public Connection getConnection(Object o) throws SQLException {
return getConnection();
}

@Override
public void releaseConnection(Object tenantIdentifier, Connection connection) throws SQLException {

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package liquibase.ext.hibernate.database;

import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;

import java.sql.Connection;
import java.sql.SQLException;

/**
* Used by hibernate to ensure no database access is performed.
*/
class NoOpConnectionProvider implements ConnectionProvider {

@Override
public Connection getConnection() throws SQLException {
throw new SQLException("No connection");
}

@Override
public void closeConnection(Connection conn) throws SQLException {
}

@Override
public boolean supportsAggressiveRelease() {
return false;
}

@Override
public boolean isUnwrappableAs(Class unwrapType) {
return false;
}

@Override
public <T> T unwrap(Class<T> unwrapType) {
return null;
}


public Connection getConnection(String tenantIdentifier) throws SQLException {

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'tenantIdentifier' is never used.
return getConnection();
}

public Connection getConnection(Object o) throws SQLException {

Check notice

Code scanning / CodeQL

Confusing overloading of methods Note

Method NoOpConnectionProvider.getConnection(..) could be confused with overloaded method
getConnection
, since dispatch depends on static types.

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'o' is never used.
return getConnection();
}

public void releaseConnection(Object tenantIdentifier, Connection connection) throws SQLException {

Check notice

Code scanning / CodeQL

Confusing overloading of methods Note

Method NoOpConnectionProvider.releaseConnection(..) could be confused with overloaded method
releaseConnection
, since dispatch depends on static types.

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'tenantIdentifier' is never used.

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'connection' is never used.

}

public void releaseConnection(String tenantIdentifier, Connection connection) throws SQLException {

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'tenantIdentifier' is never used.

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'connection' is never used.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package liquibase.ext.hibernate.database;

import org.hibernate.engine.jdbc.connections.spi.MultiTenantConnectionProvider;

import java.sql.Connection;
import java.sql.SQLException;

/**
* Used by hibernate to ensure no database access is performed.
*/
class NoOpMultiTenantConnectionProvider implements MultiTenantConnectionProvider {

@Override
public boolean isUnwrappableAs(Class unwrapType) {
return false;
}

@Override
public <T> T unwrap(Class<T> unwrapType) {
return null;
}

@Override
public Connection getAnyConnection() throws SQLException {
return null;
}

@Override
public void releaseAnyConnection(Connection connection) throws SQLException {

}

public Connection getConnection(String s) throws SQLException {

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 's' is never used.
return null;
}

public void releaseConnection(String s, Connection connection) throws SQLException {

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 's' is never used.

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'connection' is never used.

}

public Connection getConnection(Object tenantIdentifier) throws SQLException {

Check notice

Code scanning / CodeQL

Confusing overloading of methods Note

Method NoOpMultiTenantConnectionProvider.getConnection(..) could be confused with overloaded method
getConnection
, since dispatch depends on static types.

Check notice

Code scanning / CodeQL

Missing Override annotation Note

This method overrides
MultiTenantConnectionProvider<>.getConnection
; it is advisable to add an Override annotation.
return null;
}

public void releaseConnection(Object tenantIdentifier, Connection connection) throws SQLException {

Check notice

Code scanning / CodeQL

Confusing overloading of methods Note

Method NoOpMultiTenantConnectionProvider.releaseConnection(..) could be confused with overloaded method
releaseConnection
, since dispatch depends on static types.

Check notice

Code scanning / CodeQL

Missing Override annotation Note

This method overrides
MultiTenantConnectionProvider<>.releaseConnection
; it is advisable to add an Override annotation.

}

@Override
public boolean supportsAggressiveRelease() {
return false;
}

}

0 comments on commit 481d2d1

Please sign in to comment.