Hibernate.connection.datasource is deprecated, but newer alternatives don't work

Hi. I bootstrap Hibernate programmatically and apply an instance of javax.sql.DataSource. Since upgrade to Hibernate 6.4 “hibernate.connection.datasource” is deprecated (see AvailableSettings.DATASOURCE javadoc), though still works. I tried to change the name to “jakarta.persistence.jtaDataSource” or “jakarta.persistence.nonJtaDataSource”, but Hibernate fails with:

10:48:54.096 [main] WARN  org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator - HHH000342: Could not obtain connection to query metadata
java.lang.UnsupportedOperationException: The application must supply JDBC connections
	at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:44) ~[hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:428) ~[hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:61) ~[hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.getJdbcEnvironmentUsingJdbcMetadata(JdbcEnvironmentInitiator.java:276) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:107) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:129) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:238) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:215) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.boot.model.relational.Database.<init>(Database.java:45) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.getDatabase(InFlightMetadataCollectorImpl.java:223) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:191) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:170) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:129) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:449) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:101) [hibernate-core-6.4.0.Final.jar:6.4.0.Final]
	at snippet.TestCache2.makeSessionFactory(TestCache2.java:368) [classes/:?]
	at snippet.TestCache2.main(TestCache2.java:169) [classes/:?]


Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementation must be provided)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:276)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:238)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:215)
	at org.hibernate.boot.model.relational.Database.<init>(Database.java:45)
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.getDatabase(InFlightMetadataCollectorImpl.java:223)
	at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:191)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:170)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:129)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:449)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:101)
	at snippet.TestCache2.makeSessionFactory(TestCache2.java:368)
	at snippet.TestCache2.main(TestCache2.java:169)
Caused by: org.hibernate.HibernateException: Unable to determine Dialect without JDBC metadata (please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a custom Dialect implementation must be provided)
	at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:191)
	at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:87)
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.getJdbcEnvironmentWithDefaults(JdbcEnvironmentInitiator.java:143)
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.getJdbcEnvironmentUsingJdbcMetadata(JdbcEnvironmentInitiator.java:348)
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:107)
	at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68)
	at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:129)
	at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
	... 11 more
1 Like

Hi @Ilia_Basin ,

I think you are facing the same problem reported [HHH-17463] - Hibernate JIRA.

We are working on it.

Dear @dreab8 thanks for update, but I am facing the same issue now with MSSQL 9.2.1.jre11 and 12.4.2.jre11 which is builtin in spring boot 3.2 I am workking with Spring boot 3.2 where Hibernate version is 6.4.4.Final and facing the same issues, as I see in the HHH-17463 the fix version was [6.4.2].

04-10-2024T13:47:03.384+04:00 ERROR [main] o.s.o.j.AbstractEntityManagerFactoryBean: Failed to initialize JPA EntityManagerFactory: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due to: Unable to determine Dialect without JDBC metadata (please set ‘jakarta.persistence.jdbc.url’ for common cases or ‘hibernate.dialect’ when a custom Dialect implementation must be provided)

When I am trying to add the dialect

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLServerDialect

this error disapears and another error facing related in driver class name

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the {host my_host_name}, port 1433 has failed. Error: “The driver received an unexpected pre-login response. Verify the connection properties and check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. This driver can be used only with SQL Server 2005 or later.”. ClientConnectionId:22de0ae3-ccf3-4e14-a485-ce331b9fa0a0

this error disapears and another warn become error related to metadata (the solution for this as I see can be spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false but the connection it self is not working after runing and throw the driver class name error

04-10-2024T13:47:03.383+04:00 WARN [main] o.h.e.j.e.i.JdbcEnvironmentInitiator: HHH000342: Could not obtain connection to query metadata java.lang.NullPointerException: Cannot invoke “org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(java.sql.SQLException, String)” because the return value of “org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.sqlExceptionHelper()” is null

Hi, were you able to find a solution? I’m facing the same issue and haven’t been able to solve this for a couple of days now