0
votes

this is my problem: Im trying to configure my data source in context.xml tom cat's field, as follow:

//C:\apache-tomcat-7.0.42\conf\context.xml 

<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>


<Resource
  name="jdbc/myDB" docBase="inv" auth="Container"
  type="javax.sql.DataSource" driverClassName="com.mysql.jdbc.Driver"
  maxActive="100" maxIdle="30" maxWait="10000"
  url="jdbc:mysql://localhost:3306/myDB?autoReconnect=true"
  username="root" password="pass"/>

</Context>

And I have in my servlet-context.xml (spring project) the configuration of hibernate:

...
<!-- JDBC Data Source -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/rhcimax"/>
    <property name="username" value="root"/>
    <property name="password" value="1234"/>
    <property name="validationQuery" value="SELECT 1"/>
</bean>

<!-- Hibernate Session Factory -->
<bean id="mySessionFactory"     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan">
    <array>
        <value>com.blah.baseProject</value>
    </array>
</property>
<property name="hibernateProperties">
  <value>
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
  </value>
</property>
</bean>

<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>

What can I do to delete the following part of the code and delegate it to tom cat's context.xml?:

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/rhcimax"/>
    <property name="username" value="root"/>
    <property name="password" value="1234"/>
    <property name="validationQuery" value="SELECT 1"/>
</bean>

Thanks in advance.

FINAL SOLUTION:

The references are: xmlns:jee="http://www.springframework.org/schema/jee", xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd"

Finally we have:

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

<jee:jndi-lookup id="myDataSource" jndi-name="java:comp/env/jdbc/myDB"/>

<!-- Hibernate Session Factory -->
<bean id="mySessionFactory"     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>
    <property name="packagesToScan">
    <array>
        <value>com.blah.baseProject</value>
    </array>
</property>
<property name="hibernateProperties">
  <value>
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
  </value>
</property>
</bean>

<!-- Hibernate Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>

<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>

Thanks alanstroop.

More information: http://static.springsource.org/spring/docs/3.3.0.BUILD-SNAPSHOT/spring-framework-reference/html/xsd-config.html

2

2 Answers

3
votes

<jee:jndi-lookup id="myDataSource" jndi-name="java:comp/env/jdbc/myDB"/>

0
votes

You can also use jee:jndi-lookup + Spring profiles. This example shows you how to use these two Spring features. Spring Profiles will allow you to use different environments without doing any modification on your application.