0

I have a working programm with MySQL, Spring and a tomcat server, but the database connection is in spring-database.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="baseController" class="com.afterguard.sailplanner.controller.BaseController">
        <property name="dao" ref="daoImpl" />
    </bean>

    <bean id="daoImpl" class="com.afterguard.sailplanner.dao.DaoImpl">
        <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://localhost:3306/sailplanner" />
        <property name="username" value="sailplanner" />
        <property name="password" value="sailplanner2" />
    </bean>

</beans>

In order to deploy this program on a server now, I would need bring this database connection to the server, e.g. the context.xml oder server.xml.

How do I do I setup my tomcat 7 server to connect to the database and tell my application, that the database connection is setup in the tomcat?

9
  • What you want its not clear ,Please clarify your requirement. Dec 21, 2015 at 14:29
  • Ok, I tried to clarify the question.
    – DonMarco
    Dec 21, 2015 at 14:30
  • What do you mean by bringing the database connection to the server? Create a database connection on it or just deploy the XML file with your application? Dec 21, 2015 at 14:31
  • what ever i have understood from your question you want to use spring-database.xml with your applicationContext.xml file if yes then you should write this code in applicationContext.xml file <import resource="spring-database.xml" /> Dec 21, 2015 at 14:36
  • 2
    Possible duplicate of How to use JNDI DataSource provided by Tomcat in Spring?
    – Bacteria
    Dec 21, 2015 at 15:38

3 Answers 3

2

You have to create a dataSource with JNDI :

First, in your spring bean configuration (spring-database.xml) add this :

 <beans:bean id="dbDataSource"  class="org.springframework.jndi.JndiObjectFactoryBean">
    <beans:property name="jndiName" value="java:comp/env/jdbc/myDB"/>
</beans:bean>

Second, create the jndi resource in the file apache-tomcat/conf/server.xml :

<Resource name="jdbc/globalDB"
  global="jdbc/globalDB"
  auth="Container"
  type="javax.sql.DataSource"
  driverClassName="com.mysql.jdbc.Driver"
  url="jdbc:mysql://localhost:3306/sailplanner"
  username="sailplanner"
  password="sailplanner2"/>

Finally, create the resource link in the file apache-tomcat/conf/context.xml :

<ResourceLink name="jdbc/myDB"
                global="jdbc/globalDB"
                auth="Container"
                type="javax.sql.DataSource" />
1
  • Thanks. Exactly what I was looking for. Worked out of the box.
    – DonMarco
    Dec 22, 2015 at 0:07
1

For Sql Server I do the following configuration:

  1. You must be using Tomcat to manage connections. To do so we will be adding data sources to the TOMCAT_HOME/conf/context.xml file

    <Context>
    <Resource
        name="jdbc/Oltp1"
        auth="Container"
        type="javax.sql.DataSource"
        driverClassName="net.sourceforge.jtds.jdbc.Driver"
        url="jdbc:jtds:sqlserver://localhost:3306/sailplanner"
        username=user
        password=password
        testOnBorrow="true"
        testOnConnect="true"
        validationQuery="SELECT 1"
        removeAbandoned="true"
        removeAbandonedTimeout="30"
        />
    </Context>
    
  2. SQL Server JDBC driver (jtds-version.jar) must be copied to the TOMCAT_HOME/lib folder (if it is not there already)

  3. If there are multiple data sources (OLTP1, OLTP2, etc) we will be adding multiple <Resource/> tags

  4. You will be using the following JNDI names to name our data sources:

    jdbc/Oltp1 – main OLTP database
    jdbc/Oltp2 – secondary database
    
  5. You will be configuring data sources so connection pool will know how to test connections and what to do with closed connections:

    testOnBorrow="true"
        testOnConnect="true"
        validationQuery="SELECT 1"
        removeAbandoned="true"
        removeAbandonedTimeout="30"
    
  6. Tomcat must be restarted when new resource is added or existing resource is modified.

application.properties file

Important! This file is deployed as a part of the classpath (src/main/resources/application.properties), not the external application.properties with externalized properties deployed to the TOMCAT_HOME/conf

  1. You will be added the following property to this file:

    spring.datasource.jndi-name=java:comp/env/jdbc/Oltp1
    
  2. You need to remove all other spring.datasource properties from the application.properties if any exist

  3. Important! You had to remove all spring.datasource properties from the external configuration file (application.properties deployed to the COMCAT_HOME/conf)

0

the database connection properties you should put in a property file and and depending upon your requirement you can change values in property file

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.databaseurl}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

    </bean>

the property file

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.databaseurl=jdbc:mysql://localhost:3306/sailplanner

jdbc.username=root
jdbc.password=root

and to include this file in xml use

<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:applications.properties</value> 

            </list>
        </property>
    </bean>
1
  • As I am using OpenShift I like to put the database connection into the context.xml of the tomcat and not have it in the application.
    – DonMarco
    Dec 21, 2015 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.