Externalize the database connection configuration
We are currently using Kundera to access several different databases, and our persistence.xml contains several persistent units. We want make our application to work in different environments, so we have to have the several persistence.xml configurations for each environment accordingly.
The first thing, we've done, was moving persistence.xml out of the project and placing it in some external directory in a classpath. Thus we could change it in accordance to each environment we need. But then we've faced with a problem.
When you use several persistence units, you have to specify all the entity classes that are used by them explicitly in persistence.xml. Otherwise they will conflict between each other. Using the schema above, we had to change our persistence.xml files for every environment each time we add a new entity class to our code. This may be a very painful process, and it should not be used this way.
I would appreciate for any advice from the Comunity of what to do in this case.
We've solved this problem using the single persistence.xml without the database access properties and the custom ClientFactory implementation, where we apply our own properties to access the db. Is there any more attractive solution?
+1 for externalizing datastore specific. I think better if we access this properties file to overwrite db configuration(if something present in persistence.xml).
Looks like this along with https://github.com/impetus-opensource/Kundera/issues/121 can be clubbed together to override all configurable properties. I guess, Custom client factory implementation can also be avoided in this way.
-Vivek
Any timeline on being able to override/externalize the configuration?
If its not being worked on, I might be able to contribute some time/code. Its definitely a blocking issue for us.
Thanks!
Please feel free to contribute, we are planning it for 2.2 (next release), but not yet started on this. -Vivek
I think the best way to solve this problem for now is allowing to redefine the jpaProperties of PersistenceProvider (it is getting this property map in createEntityManagerFactory), but initializeKundera method does not use this map and configures the persistence units itself (see KunderaPersistence.java:81). Spring Persistence, for example, in its turn allows to redefine this property like so:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="rdbms"/>
<property name="dataSource" ref="rdbmsDataSource"/>
<property name="persistenceXmlLocation" value="classpath*:/META-INF/persistence.xml"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
</props>
</property>
</bean>
It would be great if we could use something like that with Kundera.
We can definitely exploit persistence.xml for the such properties and build such support in kundera, but i guess in case where u don't want to modify your source code every time for these set of properties, another alternative can be via some configuration file. So i guess we might need to build both ways.
-Vivek
I would also vote for the approach suggested by 'smecsia', wherein Kundera should allow overriding the properties through createEntityManagerFactory. This should be a doable fix and can be targeted for 2.2 release slotted for mid-November.
Fix is available in current trunk branch.
Commit: a0346c5fa7548670830454cb30e7555a1f3a4bca d12d288d5e2d7a5a7d27eb9c5c17e9cb067d63a9
Anybody got a chance to verify this fix? @smecsia,@machadolab
Thank you, mevivs! I can confirm that now I'm able to override the jpa properties and can move this configuration outside the persistence.xml :).
Sorry for the delay. Was on vacation and then pulled on another project. I can confirm the fix is "mostly working" :)
Specifically, it looks like host, node, dialect are working, but it looks like the kundera.client.lookup.class item cannot be supplied only by the external properties method.
Basically, I want to configure 100% of kundera from code, so a more or less empty persistence.xml file (just the definition of the persistence unit basically. When I tried to run in this mode, my external properties map has this key, but I get this exception:
java.lang.IllegalArgumentException: kundera.client property is missing for persistence unit:KunderaExamples
but it looks like the kundera.client.lookup.class item cannot be supplied only by the external properties method.
<Vivek>I think that should work as well </Vivek>
Please share code snippet to look into this.
-Vivek
Not sure what you are saying. Are you saying you think it should work or that it cannot be made to work like I am expecting?
Here is my code:
//hd is my pojo containing connection info
Map<String, Object> props = new HashMap<String, Object>();
props.put("kundera.nodes", hd.getHostname());
props.put("kundera.port", hd.getPort().toString());
props.put("kundera.keyspace", hd.getNamespace());
props.put("kundera.dialect","cassandra");
props.put("kundera.client.lookup.class","com.impetus.client.cassandra.thrift.ThriftClientFactory");
logger.debug("Attempting to connect to server [{}] at [{}].", clusterName, hd.getHostname());
try
{
tmpEntityManager = Persistence.createEntityManagerFactory(hd.getNamespace(), props)
.createEntityManager();
}
catch (Exception e)
{
logger.error(String.format("Exception thrown while trying to create entity manager for host definition [%s]: %s", hd.getId(), e));
}
and the persistence.xml:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="KunderaExamples">
<provider>com.impetus.kundera.KunderaPersistence</provider>
<properties>
</properties>
</persistence-unit>
</persistence>
Resulting in:
java.lang.IllegalArgumentException: kundera.client property is missing for persistence unit:KunderaExamples
Updated my previous comment. I love github comment box -:) Assigning to KK. I believe it should work
-Vivek
Fixed in trunk,
issue was in loadEntityMetadata method of MetamodelConfiguration.java class,
when there is no
Thanks Kuldeep
On Tue, Jan 8, 2013 at 1:02 PM, Vivek Mishra [email protected]:
Assigning to KK. I believe it should work
-Vivek
— Reply to this email directly or view it on GitHubhttps://github.com/impetus-opensource/Kundera/issues/133#issuecomment-11987359.
Thanks and Regards Kuldeep Kumar Mishra +919540965199
Great, working now! Thanks!
Cool. Sounds like it is finally approaching towards closure :+1: It's a good add-on for Kundera.
-Vivek
Working fine. Releasing with 2.3