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

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

Related

Closing conection from pool in runtime oracle-data-source

I have legacy web application. Witch uses jdbcTemplate to execute stored procedures from DB (Oracle), procedures located at packages. Problem is, when database developers compiles that packages im getting Expceptions sometihng like existing state of packages been changed (ora 4061). and they does that 1 or 2 times per week. Then i have to restart application to restart pool. app uses OracleDataSource. i can't change anything at that db but can change pool manager. so the question is. is it possible to close that connection? not just return to pool. and get new connection(from pool) if that Exception apears? thanks !!
people sugested to re-execute that stored procedure when that error apears. It doest worked.
<bean id="ds_name" class="oracle.jdbc.pool.OracleDataSource"
destroy-method="close">
<property name="connectionCachingEnabled" value="true" />
<property name="URL" value="****" />
<property name="user" value="user" />
<property name="password" value="pass" />
<property name="connectionCacheProperties">
<value>
MinLimit:1
MaxLimit:100
InitialLimit:1
ConnectionWaitTimeout:120
InactivityTimeout:180
ValidateConnection:true
</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds_name" />
</bean>

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.

Mysql configuration with spring

I have read Spring Fundamentals. Actually i visited many sites to know how to configure mysql database to spring project. But i have failed to get specific solution about it. so please help me to solve this problem.
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/database" />
<property name="username" value="user" />
<property name="password" value="pass" />
</bean>
Springframework documentation
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jdbc.html#jdbc-datasource

How to limit number of connections to a database in spring?

I have a Spring based application, deployed on the Tomcat server. What I need is to limit the maximum number of simultaneous connections to a database. This is data source section from my applicationContext.xml:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="..." />
<property name="username" value="..." />
<property name="password" value="..." />
</bean>
I would like to do it on the application level, not with server configuration.
Set the maxActive property on the dataSource.

Embedded Derby Db in a Spring app on Tomcat

I'm trying to get an embedded Derby db running on a Tomcat/Spring application.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="url" value="jdbc:derby:pepper" />
<property name="initialSize" value="5" />
<property name="maxActive" value="50" />
</bean>
When I run this, I'm getting the following error:
org.apache.commons.dbcp.SQLNestedException:
Cannot create
PoolableConnectionFactory (Database
'WEB-INF/pepper' not found.)
I've tried the pepper folder at both %webapp_root%/pepper and %webapp_root%/WEB-INF/pepper
Suggestions?
If you're deploying a web app to Tomcat, I'd recommend setting up a JNDI connection pool and using Spring's JndiObjectFactoryBean:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/blah"/>
</bean>
I guess you need to replace url with jdbc:derby:pepper;create=true

Categories

Resources