spring-cloud load datasource bean earlier than parse property placeHolder - spring-boot

After I import spring-cloud-context jar into my spring-boot2 project, it failed to parse property placeHolder in my datasource bean "org.apache.tomcat.jdbc.pool.DataSource" config. But the same property placeHolder in other bean config is parsed successfully.
Other datasource bean like "com.alibaba.druid.pool.DruidDataSource" also has the same problem. Which configuration class in spring-cloud-context cause the spring load datasource bean so eagerly?
spring config:
<bean id="dataSourceTarget" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="false" />
<property name="validationInterval" value="60000" />
<property name="validationQuery" value="select 1 from dual" />
<property name="maxActive" value="100" />
<property name="minIdle" value="1" />
</bean>
<bean id="configTest1" class="wzp.rest.service.ConfigTest"
primary="true">
<property name="password" value="${mysql.password}" />
</bean>
pom.xml:
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.4.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-archaius</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-archaius</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-ribbon</artifactId>
</exclusion>
<exclusion>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-hystrix</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>

I used MapperFactoryBean of mybatis to load mapper, and MapperFactoryBean load sqlSessionFactory and datasource bean earlier than PropertySourcesPlaceholderConfigurer changing the placeholder.
When I use org.mybatis.spring.mapper.MapperScannerConfigurer to load mapper, the problem gone.
#ConditionalOnMissingBean makes all factory beans and their dependent beans initialed early, and their getObjectType function called before placeholders in their definition was parsed. Then spring cloud config client in spring-cloud-context jar makes normal PropertySourcesPlaceholderConfigurer can not parse and update placeholders of these beans. This behavior can be forbade by setting for whom do not use cloud config:
spring.cloud.config.enabled=false
spring.cloud.refresh.enabled=false

Related

Springboot 2.2.1RELEASE -required a bean of type 'org.springframework.jdbc.core.jdbctemplate'

Currently am working on Spring boot version 2.1.7.Now i moved to Spring boot version 2.2.1 RELEASE. when i run my jar file its failed and its showing an error like
required a bean of type 'org.springframework.jdbc.core.jdbctemplate'
pom.xml
<dependencies>
<!-- Spring boot framework -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
</dependencies>
Java Class file
#Component
public class StoredProcedureExecutor
{
#Autowired
JdbcTemplate jdbc;
}
module-context.xml
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
depends-on="dalConfiguration" destroy-method="close">
<property name="driverClassName" value="#{dalConfiguration.driverClassName}" />
<property name="url" value="#{dalConfiguration.url}" />
<property name="username" value="#{dalConfiguration.username}" />
<property name="password" value="#{dalConfiguration.password}" />
<property name="initialSize" value="#{dalConfiguration.poolInitialSize}" />
<property name="maxIdle" value="#{dalConfiguration.poolMaxIdle}" />
<property name="maxActive" value="#{dalConfiguration.poolMaxActive}" />
<property name="timeBetweenEvictionRunsMillis" value="1800" />
<property name="minEvictableIdleTimeMillis" value="1800" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
I have tried below solutions.
Added spring-boot-starter-jdbc dependencies in pom.xml file.
Remove .m2\repository\org\springframework\spring-jdbc and update project
But still getting the same error.Is there any spring boot version issue?

Encountering Instantiation error while configuring HikariCP

Hi all i'm new to connection pooling.Before HikariCP i used DBCP for connection pooling. But since HikariCP is new and faster i thought of implementing the same in my project, but encountering below error while config HikarCP in spring.Pls help me overcome this.Thank You.
Error
Instantiation of bean failed:nested exception is java.lang.ExceptionInitializerError
pom.xml
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
<scope>compile</scope>
</dependency>
context.xml
<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
<property name="driverClassName"
value="oracle.jdbc.pool.OracleDataSource" />
<property name="URL" value="<url goes here>" />
<property name="user" value="abc"/>
<property name="password" value="xyz"/>
<property name="implicitCachingEnabled" value="true"/>
</bean>
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-
method="close">
<constructor-arg ref="hikariConfig" />
</bean>

Spring Security connecting authentification with a database

I'm new to spring security with tomcat, i've beeing following a video, and i did everything there but when i try to login i get a HTTP 500 Error:
with the exception: javax.servlet.ServletException: Filter execution threw an exception
and root cause : java.lang.NoClassDefFoundError: Could not initialize class org.springframework.jdbc.core.StatementCreatorUtils
Here is my spring-config file:
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService" />
</authentication-manager>
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url" value="jdbc:mysql://localhost:3306/formation" />
<beans:property name="username" value="root" />
<beans:property name="password" value="" />
</beans:bean>
<beans:bean id="userDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
<beans:property name="dataSource" ref="dataSource"/>
</beans:bean>
and the pom:
.
.
.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
.
.
.
the error says NoClassDefFoundError but i don't know about what class is he talking about?
what am i doing wrong here?

Spring Hibernate Exception for PostgressSQL Dilect

I am new to hibernate and Spring and looking to get a simple example working. Here is my spring xml configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byType">
<bean id="duke" class="com.springinaction.springidol.Juggler">
<constructor-arg value="15" />
</bean>
<bean id="kenny" class="com.springinaction.springidol.Instrumentalist">
<property name="song" value="Jingle Bells" />
</bean>
<bean id="sonet29" class="com.springinaction.springidol.Sonnet29" />
<bean id="stage" class="com.springinaction.springidol.Stage"
factory-method="getInstance" />
<bean id="saxophone" class="com.springinaction.springidol.Saxophone" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://ec2-21-21-214-251.compute-1.amazonaws.com:5432/d7zxdletq2mmv'" />
<property name="username" value="eqwosdacrklhxpq" />
<property name="password" value="wkAasO8d-hdr2hGoQXYadbjMKhGR" />
<property name="initialSize" value="1" />
<property name="maxActive" value="1" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.springinaction.springidol.models" />
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
</props>
</property>
</bean>
</beans>
My pom.xml dependencies are following
<properties>
<spring.version>3.1.1.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency> <!-- Used for Hibernate4 LocalSessionFactoryBean -->
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- AOP dependency -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<!-- Persistence Management -->
<dependency> <!-- Apache BasicDataSource -->
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency> <!-- Hibernate -->
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.3.Final</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.2-1002-jdbc4</version>
</dependency>
</dependencies>
My application is a simple command line application.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/springinaction/springidol/testing.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
What am i doing wrong. I would like to add that my models directory is empty.
Kind Regards
In the part that you define Hibernate properties, it should be like the following. All keys should be prefixed with hibernate.:
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
</props>
</property>

My Spring application leaks database connections whereas I use the default Roo configuration

I am encountering an serious issue with my application. It leaks database connections whereas I use the default Spring Roo datasource configuration as follows:
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="true" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="1800000" />
<property name="numTestsPerEvictionRun" value="3" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="validationQuery" value="SELECT 1" />
<property name="maxActive" value="2"/>
<property name="logAbandoned" value="true"/>
<property name="removeAbandoned" value="true"/>
</bean>
Here is the controller method that causes the leak:
#RequestMapping(value = "getMessages", method = RequestMethod.GET, produces = "application/json")
#ResponseBody
public DeferredResult<List<Message>> getMessages(#RequestParam final Long senderId) {
// TODO: check that recipientId was not changed by malicious user!!
final Long recipientId = memberService.retrieveCurrentMember().getId();
final String messageRequestKey = new StringBuilder().append(senderId).append(":").append(recipientId).toString();
final DeferredResult<List<Message>> deferredResult = new DeferredResult<List<Message>>(null, Collections.emptyList());
messageRequests.put(messageRequestKey, deferredResult);
deferredResult.onCompletion(new Runnable() {
#Override
public void run() {
messageRequests.remove(messageRequestKey);
}
});
List<Message> unReadMessages = messageService.findUnreadMessages(senderId, recipientId);
if (!unReadMessages.isEmpty()) {
deferredResult.setResult(unReadMessages);
}
return deferredResult;
}
It appears that connections are not returned to the pool by the above method (which is polled continuously by ajax).
Can anyone please help?
EDIT:
Hibernate config:
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<property name="hibernate.connection.release_mode" value="after_transaction"/>
</properties>
</persistence-unit>
Hibernate version:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.8.Final</version>
<exclusions>
<exclusion>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</exclusion>
<exclusion>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
I'm not sure if you are using hibernate? If so take a look at the setting hibernate.connection.release_mode . We had this issue and putting it to after_transaction solved all our connection issues.
EDIT
Our config:
<beans:bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="dataSource" ref="mvcDatasource" />
<beans:property name="persistenceUnitName" value="mvcPersistenceUnit" />
<beans:property name="persistenceProvider">
<beans:bean class="org.hibernate.ejb.HibernatePersistence" />
</beans:property>
<!-- Fix Hibernate not properly connected with spring -->
<beans:property name="jpaVendorAdapter">
<beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</beans:property>
<beans:property name="jpaPropertyMap">
<beans:map>
<!-- Connection release fix -->
<beans:entry key="hibernate.connection.release_mode" value="after_transaction" />
<beans:entry key="hibernate.dialect" value="${hibernate.dialect}" />
...
</beans:map>
</beans:property>
</beans:bean>

Resources