Overriding Hibernate ConnectionProvider in Spring Boot 2.1 - java

I am upgrading a kotlin Spring Boot 1.x application to use Spring Boot 2.1.6
Previously I used EntityManagerFactoryBeanCallback to set the hibernate.connection.provider_class to our custom implementation of ConnectionProvider. This was deprecated and removed in spring boot 2.0 and 2.1 respectively.
I am now attempting to use a HibernatePropertiesCustomizer
#Bean
fun hibernatePropertiesCustomizer() = HibernatePropertiesCustomizer {
it["hibernate.connection.provider_class"] = ScopedConnectionProvider::class.java.name
}
The application throws an error now:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by:
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by:
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
If I comment out the it["hibernate.connection.provider_class"] = ScopedConnectionProvider::class.java.name line everything works except obviously the custom ConnectionProvider
For reference I am using the ScopedConnectionProvider to enable our multitenancy by setting a variable on each connection representing the customers scope ID. Any alternate solution that allows me to run a SET #scope_id = ? query on every connection would also be fine.
Relevant version information:
Spring Boot: 2.1.6
Spring: 5.1.8
Hibernate: 5.3.10
Kotlin: 1.3.21

It turns out that the Datasource was not being injected into my ScopedConnectionProvider correctly anymore. I changed it so that I was inheriting from DatasourceConnectionProviderImpl instead of implementing my own ConnectionProvider from scratch. This seems to have removed the need to specify the dialect.

Related

Spring 6 PostGIS integration

This question was migrated from Geographic Information Systems Stack Exchange because it can be answered on Stack Overflow.
Migrated 14 days ago.
I have been trying to port an existing application using PostGIS from Spring 5 to Spring 6.
I get an error saying that org.hibernate.spatial.dialect.postgis.PostgisDialect is not more available:
Task :packages:discover-app-back-api:bootRun FAILED
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.spatial.dialect.postgis.PostgisDialect] as strategy [org.hibernate.dialect.Dialect]
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.spatial.dialect.postgis.PostgisDialect]
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.spatial.dialect.postgis.PostgisDialect
Spring boot 3.0.2
Spring security 6.0.1
Hibernate 6.1.6
Indeed, this class is no more available in the version 6.X.
What configuration should I use?
You no longer need to specify the dialect:
The PostgisDialect class was first replaced by PostgisPG9xDialect / PostgisPG10Dialect classes, but now these are also deprecated ("A SpatialDialect is no longer required.").
So the correct dialect is the default (org.hibernate.dialect.PostgreSQLDialect).
But since it is the default it shouldn't be necessary to specify it in your Spring-Boot application, both the default driver and dialect can be derived from what you specify in spring.datasource.url

Getting error to failed to look up JNDI DataSource in WebLogic

I'm using WebLogic 12.2.1.4, I have already created the datasource with the name "jdbc/TESTDataSource but still getting the below error. Is there are any additional steps that I'm missing here. Please help.
Caused by: org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'java:comp/env/jdbc/TESTDataSource'; nested exception is javax.naming.NameNotFoundException: While trying to look up comp/env/jdbc/TESTDataSource in /app/webapp/TEST.war/1431676420.; remaining name 'comp/env/jdbc/TESTDataSource'

Unable to define properties which start with keycloak

Unable to define properties which start with keycloak. keycloak-spring-boot-starter starter jar fails to load the properties if it observes keycloak properties which aren't known to the jar. The issue gets resolved if we prefix "sso" to the properties, is there a way to avoid this error even when keeping the keycloak prefixes.
keycloak.admin.username=
keycloak.admin.password=
sso.keycloak.admin.username=
sso.keycloak.admin.password=
this is the error seen with Spring Boot 2.5.0 and Keycloak 13.0.0
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
... 21 more
Caused by: java.lang.NullPointerException
at org.springframework.security.config.annotation.web.builders.HttpSecurity.addFilterAtOffsetOf(HttpSecurity.java:2654)
at org.springframework.security.config.annotation.web.builders.HttpSecurity.addFilterAfter(HttpSecurity.java:2645)
at org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter.configure(KeycloakWebSecurityConfigurerAdapter.java:123)
at com.foo.config.KeycloakSecurityConfig.configure(KeycloakSecurityConfig.java:36)
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.getHttp(WebSecurityConfigurerAdapter.java:217)
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:315)
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:93)
at com.foo.config.KeycloakSecurityConfig$$EnhancerBySpringCGLIB$$501f46fb.init(<generated>)
at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.init(AbstractConfiguredSecurityBuilder.java:338)
at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:300)
at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:38)
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.springSecurityFilterChain(WebSecurityConfiguration.java:127)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
This error was introduced in version 5.5.0 of Spring Security which comes with Spring Boot 2.5.0.
This is happening because KeyCloak adds two filters successively after another, like this:
#Override
protected void configure(HttpSecurity http) throws Exception {
http
...
.addFilterAfter(keycloakSecurityContextRequestFilter(), SecurityContextHolderAwareRequestFilter.class)
.addFilterAfter(keycloakAuthenticatedActionsRequestFilter(), KeycloakSecurityContextRequestFilter.class)
...
}
And there's a bug right now in which the custom filter order is not being persisted, thus resulting in a NullPointerException when trying to add a filter relative to another custom filter recently added.
What I advise you to do right now is to use a lower version of Spring Boot, like 2.4.x until the issue in GitHub gets resolved.
From the Keycloak code it seems they have hardwired the properties to specific properties only.
From KeycloakSpringBootProperties
/* this is a dummy property to avoid re-rebinding problem with
property keycloak.config.resolver
when using spring cloud - see KEYCLOAK-2977 */
From their code, its seems we can make use of a workaround they have to include additional properties.
But to do that we have to add 'config' in it.
keycloak.config.admin.username=asd
keycloak.config.admin.password=sss

Websphere with Oracle DB deployment Error NosuchMethod

I am trying to deploy war file on websphere 8.5.5.13. which is connected to oracle(11g) db and even i had created data sources connection on websphere.
The application is getting installed but when i try to start the application gives me the following error:
[29/10/18 15:00:49:733 IST] 000001e1 SystemErr R Caused by: com.ibm.ws.webcontainer.exception.WebAppNotLoadedException: Failed to load webapp: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax/persistence/Table.indexes()[Ljavax/persistence/Index; (loaded from file:/C:/Program Files (x86)/IBM/WebSphere/AppServer/plugins/javax.j2ee.persistence.jar by org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader#180c62e2) called from class org.hibernate.cfg.annotations.EntityBinder (loaded from file:/C:/Program%20Files%20(x86)/IBM/WebSphere/AppServer/profiles/DefaultAppServer/installedApps/AntivirusServerNode01Cell/cream_war.ear/cream.war/WEB-INF/lib/hibernate-core-5.0.12.Final.jar by
[29/10/18 15:00:49:733 IST] 000001e1 SystemErr R Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax/persistence/Table.indexes()[Ljavax/persistence/Index; (loaded from file:/C:/Program Files (x86)/IBM/WebSphere/AppServer/plugins/javax.j2ee.persistence.jar by org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader#180c62e2) called from class org.hibernate.cfg.annotations.EntityBinder (loaded from file:/C:/Program%20Files%20(x86)/IBM/WebSphere/AppServer/profiles/DefaultAppServer/installedApps/AntivirusServerNode01Cell/cream_war.ear/cream.war/WEB-INF/lib/hibernate-core-5.0.12.Final.jar by
I also tried changing the version of hibernate and jpa but that too didnt work.
But when i deploy application without using oracle db (using h2 or derby) it runs properly.
Please help me out with this error.
Not able to find the path
The NoSuchMethodError indicates that Hibernate is trying to call a method on javax.persistence.Table that doesn't exist. The Table class has been loaded from file:/C:/Program Files (x86)/IBM/WebSphere/AppServer/plugins/javax.j2ee.persistence.jar so it's WAS itself that's providing the incompatible class.
By default, WAS 8.5.5 uses JPA 2.0 but it is possible to use JPA 2.1 by following the steps described here. You've already done most of the steps described in "Embedding a third-party persistence provider within an application". The one remaining step is to configure your application's class loader order so that it will prefer classes bundled with your application to those provided by the application server.

Deploying Grails 3 App to Tomcat 8 on Elastic Beanstalk

I'm trying to deploy a Grails 3 app to a Tomcat 8 instance on Elastic Beanstalk and I've tried a few things that I've found on Google and Stack Exchange including changing the "org.springframework.boot:spring-boot-starter-tomcat" dependency from "compile" to "provided," which didn't work. Elastic Beanstalk will accept the war file, but then just shows a blank page.
I opened up the war file that Grails produced and saw that there's no index.jsp file in the base directory or web.xml file under WEB-INF. Both of those exists in a simple war file I made using a Maven web-app archetype, which works, so I have two questions:
1) Has anyone else deployed an app from the latest version of Grails to Tomcat 8? Especially on Elastic Beanstalk and especially recently? Most of the answers I've found from Googling are old, so I'm thinking maybe something has changed with a new version.
2) What's the best reference to see the process Tomcat 8 goes through when opening a WAR file and loading it? I think if I could dig deeper into this process, I could figure out where the missing pieces are.
#John Stanford , I found same problem my grails version is 3.2.3
I have tested deploy to my tomcat8 on local , and found my data-source configuration cause this problem , my grails app is just dummy app , no need database connection ,
the log /var/lib/tomcat8/logs/catalina.2017-xx-xx.log show as below
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'grailsCacheFilter': Cannot create inner bean '(inner bean)#7cbf54fc' of type [grails.plugin.cache.web.filter.simple.MemoryPageFragmentCachingFilter] while setting bean property 'filter'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#7cbf54fc': Unsatisfied dependency expressed through method 'setUrlMappingsHandlerMapping' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'urlMappingsHandlerMapping': Unsatisfied dependency expressed through method 'setWebRequestInterceptors' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'openSessionInViewInterceptor': Cannot resolve reference to bean 'hibernateDatastore' while setting bean property 'hibernateDatastore'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDatastore': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.grails.orm.hibernate.HibernateDatastore]: Constructor threw exception; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:129)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1486)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1231)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
so I comment out the production.dataSource.url of application.yml
environments:
development:
dataSource:
dbCreate: create-drop
url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
test:
dataSource:
dbCreate: update
url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
production:
grails:
serverURL: http://xxx-evn-yyy-zzzz-aws-zone-2.elasticbeanstalk.com
dataSource:
dbCreate: none
#url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
properties:
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
and finally deploy to AWS Beanstalk and it works !

Categories