-
Notifications
You must be signed in to change notification settings - Fork 375
HHH-19280: reproducer #493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
victornoel
wants to merge
1
commit into
hibernate:main
Choose a base branch
from
victornoel:HHH-19280-reproducer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/MyConnectionProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package org.hibernate.bugs; | ||
|
||
import com.zaxxer.hikari.*; | ||
import io.opentelemetry.api.*; | ||
import io.opentelemetry.instrumentation.jdbc.datasource.*; | ||
import org.hibernate.*; | ||
import org.hibernate.dialect.*; | ||
import org.hibernate.engine.jdbc.connections.internal.*; | ||
import org.hibernate.engine.jdbc.connections.spi.*; | ||
import org.hibernate.hikaricp.internal.*; | ||
import org.hibernate.internal.log.*; | ||
import org.hibernate.internal.util.*; | ||
import org.hibernate.service.*; | ||
import org.hibernate.service.spi.*; | ||
|
||
import javax.sql.*; | ||
import java.sql.*; | ||
import java.util.*; | ||
|
||
import static org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.allowJdbcMetadataAccess; | ||
|
||
/** | ||
* Copy paste from {@link HikariCPConnectionProvider} because I couldn't find another | ||
* way of wrapping a ds without having to parse the url and configuration ^^ | ||
*/ | ||
public class MyConnectionProvider implements ConnectionProvider, Configurable, Stoppable { | ||
|
||
private static final long serialVersionUID = -9131625057941275711L; | ||
private boolean isMetadataAccessAllowed = true; | ||
|
||
/** | ||
* HikariCP configuration. | ||
*/ | ||
private HikariConfig hcfg = null; | ||
|
||
/** | ||
* HikariCP data source. | ||
*/ | ||
private HikariDataSource hds = null; | ||
|
||
private DataSource ds = null; | ||
|
||
// ************************************************************************* | ||
// Configurable | ||
// ************************************************************************* | ||
|
||
@Override | ||
public void configure(Map<String, Object> props) throws HibernateException { | ||
try { | ||
isMetadataAccessAllowed = allowJdbcMetadataAccess( props ); | ||
|
||
ConnectionInfoLogger.INSTANCE.configureConnectionPool( "HikariCP" ); | ||
|
||
hcfg = HikariConfigurationUtil.loadConfiguration( props ); | ||
hds = new HikariDataSource( hcfg ); | ||
|
||
// this is the DataSource that will not always work as expected by hibernate | ||
// see https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/13580 | ||
ds = JdbcTelemetry.builder(OpenTelemetry.noop()) | ||
.build() | ||
.wrap(hds); | ||
} | ||
catch (Exception e) { | ||
ConnectionInfoLogger.INSTANCE.unableToInstantiateConnectionPool( e ); | ||
throw new HibernateException( e ); | ||
} | ||
} | ||
|
||
// ************************************************************************* | ||
// ConnectionProvider | ||
// ************************************************************************* | ||
|
||
@Override | ||
public Connection getConnection() throws SQLException { | ||
return ds != null ? ds.getConnection() : null; | ||
} | ||
|
||
@Override | ||
public void closeConnection(Connection connection) throws SQLException { | ||
connection.close(); | ||
} | ||
|
||
@Override | ||
public boolean supportsAggressiveRelease() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public DatabaseConnectionInfo getDatabaseConnectionInfo(Dialect dialect) { | ||
return new DatabaseConnectionInfoImpl( | ||
hcfg.getJdbcUrl(), | ||
// Attempt to resolve the driver name from the dialect, in case it wasn't explicitly set and access to | ||
// the database metadata is allowed | ||
!StringHelper.isBlank( hcfg.getDriverClassName() ) ? hcfg.getDriverClassName() : extractDriverNameFromMetadata(), | ||
dialect.getVersion(), | ||
Boolean.toString( hcfg.isAutoCommit() ), | ||
hcfg.getTransactionIsolation(), | ||
hcfg.getMinimumIdle(), | ||
hcfg.getMaximumPoolSize() | ||
); | ||
} | ||
|
||
private String extractDriverNameFromMetadata() { | ||
if (isMetadataAccessAllowed) { | ||
try ( Connection conn = getConnection() ) { | ||
DatabaseMetaData dbmd = conn.getMetaData(); | ||
return dbmd.getDriverName(); | ||
} | ||
catch (SQLException e) { | ||
// Do nothing | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean isUnwrappableAs(Class<?> unwrapType) { | ||
return ConnectionProvider.class.equals( unwrapType ) | ||
|| HikariCPConnectionProvider.class.isAssignableFrom( unwrapType ) | ||
|| DataSource.class.isAssignableFrom( unwrapType ); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> T unwrap(Class<T> unwrapType) { | ||
if ( ConnectionProvider.class.equals( unwrapType ) | ||
|| HikariCPConnectionProvider.class.isAssignableFrom( unwrapType ) ) { | ||
return (T) this; | ||
} | ||
else if ( HikariDataSource.class.isAssignableFrom( unwrapType ) ) { | ||
return (T) hds; | ||
} | ||
else if ( DataSource.class.isAssignableFrom( unwrapType ) ) { | ||
return (T) ds; | ||
} | ||
else { | ||
throw new UnknownUnwrapTypeException( unwrapType ); | ||
} | ||
} | ||
|
||
// ************************************************************************* | ||
// Stoppable | ||
// ************************************************************************* | ||
|
||
@Override | ||
public void stop() { | ||
if ( hds != null ) { | ||
ConnectionInfoLogger.INSTANCE.cleaningUpConnectionPool( "HikariCP" ); | ||
hds.close(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you please add
to the class?
Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can but actually is this correct? it's a copy paste of
HikariCPConnectionProvider
which have:Also, ideally, if you know of a way to do without hikaricp, it would be even better :) is there some idiomatic way of wrapping the
DataSource
used in the tests without implementing my ownConnectionProvider
for example?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok I'll try to figure it out. Thanks again for the reproducer.