Spring Beans: DriverManagerDataSource converting to sun.jdbc.odbc.ee.DataSource - java

I have a Spring MVC project that I am using NamedParameterJdbcTemplate to connect to a postgres database.
When I try to launch my application, I get an exception:
Cannot convert value of type
[org.springframework.jdbc.datasource.DriverManagerDataSource]
to required type [sun.jdbc.odbc.ee.DataSource] for
property 'dataSource': no matching editors or conversion strategy found
My dataSource bean looks like this:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/********" />
<property name="username" value="postgres" />
<property name="password" value="********" />
</bean>
I have connected to postgres with NamedParameterJdbcTemplate before. Am I missing a dependency that makes this conversion possible, or what is the problem?

As the exception itsel reveals that it is expecting sun.jdbc.odbc.ee.DataSource
and found org.springframework.jdbc.datasource.DriverManagerDataSource
That implies you have wrong import wherever you are using DataSource class make sure it is imported from javax.sql.DataSource

Related

Configure SessionFactory of JPA into Spring

I am trying to add a Jar to my project that uses Spring 4 sessionFactory to connect to DB. My Project uses Spring boot with JPA for transactions.
How do I configure my project in order that the JPA sessionFactory is passed to the Spring sessionFactory? .
When I put the jar in my project, I got the error: "Consider defining a bean named 'sessionFactory' in your configuration."
So, I add a sessionFactory on my applicationContext.xml:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${spring.datasource.driver-class-name}" />
<property name="url" value="${spring.datasource.url}" />
<property name="username" value="${spring.datasource.username}" />
<property name="password" value="${spring.datasource.password}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.secondProjectClass</value>
</list>
</property>
</bean>
But, when I add this, the following error appears:
Action:
Consider defining a bean named 'entityManagerFactory' in your configuration.
My projects has spring-boot-starter-data-jpa:2.2.4.RELEASE, and the jar uses Spring 4.
Currently I am not able to edit the second jar (the one with spring 4).
Is this configuration possible? Thanks!
First project config: (Base project)
implementation ('org.springframework.boot:spring-boot-starter-data-jdbc')
implementation ('org.springframework.boot:spring-boot-starter-web')
compile ('org.springframework.boot:spring-boot-starter-data-jpa:2.2.4.RELEASE')
Second project config: (Jar that I need to add)
springVersion = '4.3.13.RELEASE'
compile("org.springframework:spring-context:${springVersion}")
compile("org.springframework:spring-web:${springVersion}")
compile("org.springframework:spring-tx:${springVersion}")
I solved this by keeping only SpringBoot + JPA Repositories, everything in the same version.

How do I instantiate AmazonS3Client via Spring?

I am trying to create a Spring boot application where I am using Amazon RDS mysql database and S3 for storing customer image.
But as I am running my main class: I am always getting
Bean of type 'com.amazonaws.services.s3.AmazonS3Client' that could not be found
Below is my project hierarchy:
My question is the spring configuration file is in the right folder?
As I find instantiate dataSource via spring xml more easy. And on the internet I haven't found single example where Spring boot Application has used Spring xml file? All are using annotations?
<bean id="customerDAO" class="com.sap.JdbcCustomerDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://rdssample.xxxxxxp.us-west-2.rds.amazonaws.com:3306/customer" />
<property name="username" value="rdssample" />
<property name="password" value="rdssample" />
</bean>
Is my spring config file at the correct path?
Please help me with the solution.

Spring MethodInvokingBean - The value returned is not accepted by DriverManagerDataSource

I have tried looking on google about the problem, but not able to get a solution.
What I am trying to achieve
See the below code, what I am trying to do is pass an encrypted password to MethodInvokingBean, which uses com.xxxxxxx.CryptoUtil to decrypt is using a static method decrypt.
The decrypted value is injected into masterDBDatasource via <property name="password" ref="decryptedDBPassword" /> , but it is not working.
<bean id="decryptedDBPassword" class="org.springframework.beans.factory.config.MethodInvokingBean">
<property name="targetClass" value="com.xxxxxxx.CryptoUtil"/>
<property name="targetMethod" value="decrypt"/>
<property name="arguments" value="${encrypted.db.password}" />
</bean>
<bean id="masterDBDatasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver.class}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" ref="decryptedDBPassword" />
</bean>
Exception
org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'org.springframework.beans.factory.config.MethodInvokingBean' to required type 'java.lang.String' for property 'password'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.springframework.beans.factory.config.MethodInvokingBean] to required type [java.lang.String] for property 'password': no matching editors or conversion strategy found
I followed the below tutorial as a reference
https://www.mkyong.com/spring/spring-methodinvokingfactorybean-example/
I also tried <property name="password" value="decryptedDBPassword" />
But the DB connection is saying - access denied due to invalid password.
Kindly help.
Posting exact answer for my problem code as a reference for other people, who might be facing similar problem.
From #Matt's Hint, I have final config as below using SPeL (without using MethodInvokingBean)
<bean id="masterDBDatasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver.class}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value='#{T(com.xxxxxxx.CryptoUtil).decrypt("${encrypted.db.password}")}' />
</bean>
The property password should be a String value. You are passing a bean (decryptedPassword) reference to the DriverManagerDataSource that expects a String as a password. It should be like
<property name="password" value="${db.password} />
similar to the username you provide.
As you need to pass the decrypted password you might want to take a look at Spring's expression support, that would let you process the password before passing it.
http://docs.spring.io/spring/docs/3.0.0.M3/spring-framework-reference/html/ch07s04.html

How do you configure a DataSource in Spring?

I am studying for Spring Core certification and I have some doubt about how correctly answer to this question:
How do you configure a DataSource in Spring? Which bean is very useful
for development?
I think that I do something like this to configure DataSource in a Spring XML configuration file:
<bean id=“dataSource” class=“org.apache.commons.dbcp.BasicDataSource”>
<property name=“url” value=“${dataSource.url}” />
<property name=“username” value=“${dataSource.username}” />
<property name=“password” value=“${dataSource.password}” />
</bean>
<jdbc:initialize-database data-source=“dataSource”>
<jdbc:script location=“classpath:schema.sql” />
<jdbc:script location=“classpath:test-data.sql” />
</jdbc:initialize-database>
So I think that the answered bean is the org.apache.commons.dbcp.BasicDataSource. Is this assertion true or am I missing something?
What exactly represent the declared configuration tag? It is clear for me what it do but what exactly represent? Is it a special bean declaration or what?
Tnx
I believe it has to be helper for creating in memory DB for DEV purposes:
#Bean
#Profile("dev")
public DataSource devDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("classpath:com/bank/config/sql/schema.sql")
.addScript("classpath:com/bank/config/sql/test-data.sql")
.build();
}
XML config:
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
Relevant part of Spring docs.
I don't know much about exams and certification questions :-)
But here is a valid XML datasource config JDBC MySQL
<!-- App's dataSource used by jdbcTemplate,jdbc-user-service and connectController anss so on -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/springappdb" />
<property name="username" value="root" />
<property name="password" value="yourpassword" />
</bean>
<!-- jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>

openshift mysql connection NumberFormatException with Tomcat 7 (JBoss EWS 2.0)

I am new to openshift. I created a sample application with spring security and tried to deploy in it. My spring-database.xml fragment looks like below.
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://${env.OPENSHIFT_MYSQL_DB_HOST}:${env.OPENSHIFT_MYSQL_DB_PORT}/${env.OPENSHIFT_APP_NAME}" />
<property name="username" value="${env.OPENSHIFT_MYSQL_DB_USERNAME}" />
<property name="password" value="${env.OPENSHIFT_MYSQL_DB_PASSWORD}" />
</bean>
and I am getting following exception in the cloud environment.
Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "${env.OPENSHIFT_MYSQL_DB_PORT}"'
The following are my catrige/gear configuration
Mysql 5.1
Tomcat 7 (JBoss EWS 2.0)
any solutions how to overcome this issue?
The problem is the usage of expression like ${env.OPENSHIFT_MYSQL_DB_HOST}.
Those are operating system environment variables and as you can see in the exception that instead of trying to read the values of those system environment variables Spring is trying to turn it into a number.
Try something like #{systemEnvironment[OPENSHIFT_MYSQL_DB_HOST]} to read the OS environment variables. That's an expression to read system environment variable called OPENSHIFT_MYSQL_DB_HOST.
For whom like me that the accpeted asnwer did not work for them I used this answer
I was just putting
<bean id="propertyPlaceholderChttps://stackoverflow.com/a/3965727/1460591onfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"></bean>
into application context with adding varibales like this
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/${OPENSHIFT_APP_NAME}" />
<property name="username" value="${OPENSHIFT_MYSQL_DB_USERNAME}" />
<property name="password" value="${OPENSHIFT_MYSQL_DB_PASSWORD}" />
</bean>
And It all worked just fine

Categories

Resources