14

I've looked over the documentation to define a bean. I'm just unclear on what class file to use for a Mysql database. Can anyone fill in the bean definition below?

<bean name="dataSource" class="">
    <property name="driverClassName" value="" />
    <property name="url" value="mysql://localhost/GameManager" />
    <property name="username" value="gamemanagertest" />
    <property name="password" value="1" />
</bean>
cyotee doge
  • 1,128
  • 4
  • 15
  • 33

3 Answers3

45
<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/GameManager" />
    <property name="username" value="gamemanagertest" />
    <property name="password" value="1" />
</bean>

http://docs.spring.io/spring-data/jdbc/docs/1.1.0.M1/reference/html/orcl.datasource.html

m-szalik
  • 3,546
  • 1
  • 21
  • 28
cyotee doge
  • 1,128
  • 4
  • 15
  • 33
7

Use this class org.springframework.jdbc.datasource.DriverManagerDataSource - DriverManagerDataSource. As a best practice its better if we isolate the database values into a .properties file and configure it into our spring servlet xml configuration. In the below example the properties are stored as key-value pairs and we access the value using the corresponding key.

applicationContext-dataSource.xml:

<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
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="connectionCachingEnabled" value="true"/>
</bean>

<context:property-placeholder location="classpath:jdbc.properties"/>

jdbc.propeties file:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sample_db
jdbc.username=root
jdbc.password=sec3ret
agilob
  • 6,082
  • 3
  • 33
  • 49
Lucky
  • 16,787
  • 19
  • 117
  • 151
  • 1
    Yes, but the use of property placeholders was not the point, and just complicates the issue. Also, why are you declaring your infrastructure beans in your presentation layer? Also, the use of property files is being replaced by using environment variables. – cyotee doge Jun 30 '15 at 11:34
  • Yes the propeties file is not the point but where can you see the presentation layer in my answer and i believe using `properties` file is a good practice rather than editing the xml configuration http://stackoverflow.com/a/883910/1793718. It allows to store multiple key-value pairs and how would you use the environment variables instead of property files? could you provide me a link? – Lucky Jun 30 '15 at 13:14
  • ["This is useful to allow the person deploying an application to customize environment-specific properties (for example database URLs, usernames and passwords), without the complexity or risk of modifying the main XML definition file or files for the container."](http://docs.spring.io/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer) – Lucky Jun 30 '15 at 13:21
  • You are correct, externalizing configuration is a best practice. I just meant that I wasn't including it so I didn't confuse the issue. It's what we do the training material. Using environment variables comes from the 12 Factors of Cloud Development. http://12factor.net Sporting Cloud uses an Environment object that Spring populates with environment variables, and properties. I will have to track down a link for Spring Cloud. But, it's useful even outside cloud development. It allows you to decouple an application from its deployment strategy. – cyotee doge Jul 01 '15 at 00:12
  • Oh, and I mentioned presentation layer because your original post showed a servlet-context.xml. I presumed that was the servlet beans, which would be the presentation layer. – cyotee doge Jul 01 '15 at 00:14
6

Both the answers are appropriate for the question. But just for an FYI if you're going to use DriverManagerDataSource as your datasource, every call to your datasource bean will create a new connection to your database which is not recommended for production and even it does not pool connections.

If you need a connection pool, consider Apache Commons DBCP.

<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/GameManager" />
    <property name="username" value="gamemanagertest" />
    <property name="password" value="1" />
    <property name="initialSize" value="2" />
    <property name="maxActive" value="5" />
</bean>

Where initialSize and maxActive are pooling related properties.

To use this make sure you have the required jar in your path.

TechnocratSid
  • 2,245
  • 1
  • 16
  • 26