Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » External file to specify DB information
External file to specify DB information [message #521325] Wed, 17 March 2010 09:53 Go to next message
SMaton  is currently offline SMaton Friend
Messages: 6
Registered: March 2010
Junior Member
Hello everyone,

I'm currently developing an application using Springframework, OSGi and EclipseLink. Currently, to store the DB connection information, I have a persistance.xml. I don't want my client to need to re-export the plugin to specify the information about his own DB.

So, perhaps this is a stupid question, but is there a way to specify the information such as eclipselink.jdbc.url, eclipselink.jdbc.user and eclipselink.jdbc.password from "outside" the actual plugin making use of those information?

Ideally I want the client to have a human readable file which he can edit (security issues set aside).

Thanks for helping,
Stefan

[Updated on: Wed, 17 March 2010 10:00]

Report message to a moderator

Re: External file to specify DB information [message #521420 is a reply to message #521325] Wed, 17 March 2010 14:34 Go to previous message
SMaton  is currently offline SMaton Friend
Messages: 6
Registered: March 2010
Junior Member
Replying to myself Smile I have found a solution:

Actually my application has 2 xml files containing information about how to connect to the database. One is the already mentioned persistence.xml, the other one is called jpa.xml.

The jpa.xml file is located in the same bundle as the persistence.xml but within a different folder. While the persistence.xml is directly located in the META-INF folder, the jpa.xml is located under META-INF/spring.

The persistence.xml contains a description for the persistence unit. Initially I had also stored the eclipselink properties such as the driver, the url or the password. Storing those values within the persistence.xml wasn't such a good idea, so (after talking to one of my friends) I decided to pull the eclipselink configuration into the jpa.xml.

The jpa.xml defines my entityManagerFactory bean. One of the properties defines the jpaVendorAdapter (which in my case is set to the eclipselink adapter). My friend suggested to directly embed the properties of the eclipselink adapter into the bean definition. He suggested that, since spring seems to map its own properties to the vendor adapter specific properties, this might be the right place to put them.

Unfortunately I could not find any valid solution to this problem because I could not find any kind of mapping table. So, after some searching, I found that you can also define something called "jpaPropertyMap" within your bean. Fortunately, this map can contain vendor specific properties. So I was able to (more or less directly) copy the properties into this description.

The next step was to enable me to "overload" the property values with settings contained in a property file. This is quite easy because you simply have to replace the map value by a placeholder ( like $(dbuser) ). Finally, I had to tell the bean how to replace the placeholders.

In the end, I came up with this content for my jpa.xml:

	<context:load-time-weaver weaver-class="org.eclipse.equinox.weaving.springweaver.EquinoxAspectsLoadTimeWeaver"/>			
	<context:property-placeholder location="classpath*:configuration/db.properties"/>

	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
		<property name="persistenceUnitName" value="CRAMS"/>
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
				<property name="showSql" value="true" />
				<property name="generateDdl" value="false" />
			</bean>
		</property>
		<property name="jpaPropertyMap">
            <map>
                <entry key="eclipselink.logging.level" value="INFO"/>
                <entry key="eclipselink.logging.exceptions" value="true"/>
                <entry key="eclipselink.target-database" value="MYSQL"/>
                <entry key="eclipselink.target-server" value="none"/>
				<entry key="eclipselink.jdbc.native-sql" value="true"/>
				<entry key="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver" />
				<entry key="eclipselink.jdbc.url" value="${dburl}" />
				<entry key="eclipselink.jdbc.user" value="${dbuser}" />
				<entry key="eclipselink.jdbc.password" value="${dbpwd}" />
            </map>
        </property>
		
		<property name="jpaDialect">
			<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect" />
		</property>
	</bean>


The content of the withcoming db.properties is straightforward, too:
dbuser=blubb
dbpwd=blab
dburl=jdbc:mysql://server1:3306/myscheme


Hopefully this helps someone else because I surely struggled on this.

Best regards,
Stefan
Previous Topic:How to load all objects in JPA 2.0 using Criteria
Next Topic:Marshalling attributes as node, why ?
Goto Forum:
  


Current Time: Mon Dec 22 20:07:08 GMT 2014

Powered by FUDForum. Page generated in 0.24421 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software