user3406210
user3406210

Reputation: 1

JBoss 7 Override JNDI Datasource

I have a Spring Web Application with Maven 3 and Datasources/Beans configured for local development on a h2 Database. For Testing there exists a JBoss AS 7.1 Server.

Is it possible to override a Bean / DataSource in a special JBoss XML-File, so that for local (IDE) matters a DriverManagerDataSource is used (see first code posting), and a JndiObjectFactoryBean (second one), if my Application is deployed on a JBoss?

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
<!-- And so on -->">

<!--GENERAL-->
<context:annotation-config/>
<context:component-scan base-package="com.mysuperapp"/>

<!--DATASOURCES-->
<bean id="activitiDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver"/>
    <property name="url" value="jdbc:h2:file:h2\activiti;MVCC=TRUE"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<bean id="hibernateDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver"/>
    <property name="url" value="jdbc:h2:file:h2\hibernate;AUTO_SERVER=TRUE"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<!--TXMANAGEMENT-->

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="mysuperappPersistenceUnit"/>
    <property name="dataSource" ref="hibernateDataSource"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
    <property name="packagesToScan" value="com.mysuperapp.model"/>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <entry key="hibernate.hbm2ddl.auto" value="update"/>
        </map>
    </property>
</bean>

<!-- And so on -->

custom applicationContext.xml datasources for JBoss

<bean id="activitiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:jboss/datasources/activitiDataSource"/>
</bean>

<bean id="hibernateDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:jboss/datasources/hibernateDataSource"/>
</bean>

Any help is greatly appreciated.

Upvotes: 0

Views: 1669

Answers (3)

user3406210
user3406210

Reputation: 1

I managed to do this with a SVN patch, which is added prior to the automatic deployment. It changes the DataSource definitions.

Pay attention when creating the patch with windows and applying under linux with /usr/bin/patch (CR+LF problem, apply dos2unix filename to both patch and files to be patched).

Upvotes: 0

NeoP5
NeoP5

Reputation: 619

Your application creates the database connection on its own as i can see on your config.

Instead of doing this you could just define the datasource on the app-server and obtain it by a jndi lookup. So you can configure the database connection for each system separately.

    <!-- DataSource-LookUp -->
 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" scope="singleton">  
    <property name="jndiName" value="java:jboss/datasources/MyDataSourceDS" />  
    <property name="resourceRef" value="true" />  
</bean>  

Upvotes: 1

Thomas
Thomas

Reputation: 88707

Your JBoss has a file <jboss_home>/standalone/configuration/standalone.xml which is used if you don't provide a different one.

For testing purposes you could try and make a copy of that file in the same directory, e.g. naming it test.xml and add your datasource definition there. Then use it by starting JBoss with standalone.bat --server-config=test or standalone.bat -c test.

Upvotes: 0

Related Questions