Embedded Derby Db in a Spring app on Tomcat - java

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

Related

Configuring postgresql driver through Spring xml datasource

I've been trying to configure the connections made with a postgresql datasource declared in a xml Spring configuration file.
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/dbname" />
<property name="username" value="postgres" />
<property name="password" value="" />
<property name="socketTimeout" value="10"/>
</bean>
I know, I shouldn't be using the DriverManagerDataSource class from spring, (we will soon move to C3p0 or DBCP) because it's not a real pooling.
I'm trying to set the socketTimeout value of a postgresql connection ( described here https://jdbc.postgresql.org/documentation/head/connect.html ) but of course, "socketTimeout" is not a property of the datasource, so it doesn't work.
Is it possible to do this through the datasource xml's configuration ? Or should I do it somewhere else ? Because the data source manages the connection I don't think I'll be able to do a
props.setProperty("timeout",30);
Connection conn = DriverManager.getConnection(url, props);
Can I even do this with the DriverManagerDataSource ? I tried to search, but I didn't find anything usefull, as not a lot of people are really using it.
Thank you M. Deinum, I was able to find how.
Actually, even knowing the property was named "connectionProperties", I didn't found a lot answers (maybe people rarely use it this way ?). So I'm posting it:
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/dbname" />
<property name="username" value="postgres" />
<property name="password" value="" />
<!--<property name="socketTimeout" value="10"/>-->
<property name="connectionProperties">
<props>
<prop key="socketTimeout">10</prop>
</props>
</property>
</bean>
If anyone has a better/more complete answer, I'll check it out ;)

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

how can i get database password at runtime before connecting to the database

in my applicationcontext.xml i have this :
<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/dashboardsupervisor" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
here i am connecting with my database :
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
MySQLRdbHelper rdbHelper = (MySQLRdbHelper)
ctx.getBean("ManagerSupervisor");
What is want is to NOT read the password "1234" from my applicationcontext.xml
and read it from some properties file in my local drive .
as this will be running on different machines and every one have different passwords.
Can i achieve this .
thanks
Yes, you can, and the key to this is Springs PropertyPlaceholderConfigurer.
For example, you create a file on your file system called database.properties, containing your password (note, that you can also add more settings to this file, like username, JDBC url, etc).
jdbc.password=1234
Next, you need to declare a PropertyPlaceholderConfigurer bean and point it to the database.properties file:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>path/to/database.properties</value>
</property>
</bean>
Note that the path is interpreted as a classpath resource, unless it is prefixed with file:.
Finally, replace the configuration of your dataSource bean: replace
<property name="password" value="1234" />
with
<property name="password" value="${jdbc.password}" />
You could use profiles:
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles("dev1");
ctx.load("*Context.xml");
ctx.refresh();
<bean id="database1" profile="dev1"/>
<bean id="database2" profile="dev2">
The best way is to create the data source in application server and configure as below in application.xml
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>YourDSName</value></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
.....
</bean>

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.

FileNotFoundException while running Spring and Ibatis based application on Tomcat

I am using Spring, ibatis for ORM. My app-config.xml look like
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://192.168.10.50/lmexdb_v1" />
<property name="username" value="lmexdba" />
<property name="password" value="lmexdba123#" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:com/platysgroup/lmex/server/mobile/dao/ibatis/SqlMapConfig.xml" />
</bean>
<bean id="mobileController" class="com.platysgroup.lmex.server.controller.MobileController">
<property name="announcementService" ref="announcementService"></property>
<property name="courseService" ref="courseService"></property>
<property name="userService" ref="userService"></property>
</bean>
and I have my sqlmapconfig.xml file in src/webapp/spring.
But when I run my application on tomcat it show me a exception:
java.io.FileNotFoundException: class path resource [com/platysgroup/lmex/server/mobile/dao/ibatis/SqlMapConfig.xml] cannot be opened because it does not exist
put it in src , and it will be available
if you are using maven project then add it to resource

Categories

Resources