Mysql configuration with spring - java

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

Related

How to provide data source entry in spring annotation based project.

I am using spring 3 but my configuration is xml based in which I am trying to convert my project into annotation based using #configuration annotation.
I am little confused how to add data source entry as #Bean for two below cases
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/myds" />
</bean>
<bean id="data_source" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="url1" />
<property name="username" value="user1" />
<property name="password" value="****" />
</bean>

JdbcHttpSessionConfiguration messes my Bean Constants

I am using spring MVC 4.2 I have this code in my app-context.xml
<bean id="beanConstants" name="beanConstants" class="com.my.web.controller.BeanConstants">
<property name="dbProplocation" value="/my/database.properties" />
<property name="extractDbProplocation" value="/my/extract.database.properties" />
<property name="cssLocation" value="uncompiled" />
<property name="enableSuspensionPollingStr" value="false" />
</bean>
<!-- JDBC Session Config -->
<bean class="org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration"/>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${hibernate.connection.url}" />
<property name="username" value="${hibernate.connection.username}" />
<property name="password" value="${hibernate.connection.password}" />
</bean>
Now this all worked fine until I added the JdbcHttpSessionConfiguration bean. If I take that line out, it works. otherwise I get this:
Caused by: java.lang.IllegalArgumentException:
Could not resolve placeholder 'hibernate.connection.url' in string value "${hibernate.connection.url}"
I am trying to save the sesstion info in the DB. Why is this doing this? What has one got to do with the other?

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 ;)

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

How to inject the driverClassLoader property in spring?

I have the following partial spring context xml file:
<bean name="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassLoader" value="" /> <!-- THIS PROPERTY -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3061/my_database" />
<property name="username" value="username" />
<property name="password" value="password" />
<property name="initialSize" value="8" />
</bean>
How do I inject the driverClassLoader property? (I'm using some custom plug-in architecture but not the spring dm server so have to provide a classloader to find the mysql driver)
this apache dbcp classloader bug was just fixed in march 2011.
It may fix your root issue so you don't need to inject the classloader...
https://issues.apache.org/jira/browse/DBCP-333
without this bug fix I don't think the driverClassloader setter was working....
I think you want to use the PropertyPlaceholderConfigurer. Look at section 3.7.2.1 in the Spring 2.0 reference guide.

Categories

Resources