You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2011/06/23 16:10:19 UTC
svn commit: r1138889 - in /openejb/trunk/openejb3/examples: ./
dynamic-datasource-routing/ dynamic-datasource-routing/src/
dynamic-datasource-routing/src/main/
dynamic-datasource-routing/src/main/java/
dynamic-datasource-routing/src/main/java/org/ dyna...
Author: rmannibucau
Date: Thu Jun 23 14:10:18 2011
New Revision: 1138889
URL: http://svn.apache.org/viewvc?rev=1138889&view=rev
Log:
adding dynamic-datasource-routing example
Added:
openejb/trunk/openejb3/examples/dynamic-datasource-routing/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/pom.xml (with props)
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/BoostrapUtility.java (with props)
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/DeterminedRouter.java (with props)
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/Person.java (with props)
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/RoutedPersister.java (with props)
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/org.router/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/org.router/service-jar.xml (with props)
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/persistence.xml (with props)
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/test/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/test/java/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/test/java/org/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/test/java/org/superbiz/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/test/java/org/superbiz/dynamicdatasourcerouting/
openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/test/java/org/superbiz/dynamicdatasourcerouting/DynamicDataSourceTest.java (with props)
Modified:
openejb/trunk/openejb3/examples/pom.xml
Added: openejb/trunk/openejb3/examples/dynamic-datasource-routing/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-datasource-routing/pom.xml?rev=1138889&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-datasource-routing/pom.xml (added)
+++ openejb/trunk/openejb3/examples/dynamic-datasource-routing/pom.xml Thu Jun 23 14:10:18 2011
@@ -0,0 +1,89 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.superbiz</groupId>
+ <artifactId>dynamic-datasource-routing</artifactId>
+ <packaging>jar</packaging>
+ <version>1.1-SNAPSHOT</version>
+ <name>OpenEJB :: Examples :: Dynamic Datasource Routing</name>
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <openejb.version>4.0.0-SNAPSHOT</openejb.version>
+ </properties>
+ <build>
+ <defaultGoal>install</defaultGoal>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <repositories>
+ <repository>
+ <id>apache-m2-snapshot</id>
+ <name>Apache Snapshot Repository</name>
+ <url>http://repository.apache.org/snapshots</url>
+ </repository>
+ </repositories>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.openejb</groupId>
+ <artifactId>javaee-api</artifactId>
+ <version>6.0-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <!--
+ The router is dependent of openejb so it needs openejb core with compile scope.
+ -->
+ <dependency>
+ <groupId>org.apache.openejb</groupId>
+ <artifactId>openejb-core</artifactId>
+ <version>${openejb.version}</version>
+ </dependency>
+ </dependencies>
+ <!--
+ This section allows you to configure where to publish libraries for sharing.
+ It is not required and may be deleted. For more information see:
+ http://maven.apache.org/plugins/maven-deploy-plugin/
+ -->
+ <distributionManagement>
+ <repository>
+ <id>localhost</id>
+ <url>file://${basedir}/target/repo/</url>
+ </repository>
+ <snapshotRepository>
+ <id>localhost</id>
+ <url>file://${basedir}/target/snapshot-repo/</url>
+ </snapshotRepository>
+ </distributionManagement>
+</project>
\ No newline at end of file
Propchange: openejb/trunk/openejb3/examples/dynamic-datasource-routing/pom.xml
------------------------------------------------------------------------------
svn:executable = *
Added: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/BoostrapUtility.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/BoostrapUtility.java?rev=1138889&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/BoostrapUtility.java (added)
+++ openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/BoostrapUtility.java Thu Jun 23 14:10:18 2011
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.dynamicdatasourcerouting;
+
+import javax.annotation.PostConstruct;
+import javax.ejb.Singleton;
+import javax.ejb.Startup;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+/**
+ * OpenJPA create the table at the first query.
+ * To avoid to have to create the table manunally this singleton will do it for us.
+ *
+ * @author Romain Manni-Bucau
+ */
+@Startup
+@Singleton
+public class BoostrapUtility {
+ @PersistenceContext(unitName = "db1")
+ private EntityManager em1;
+
+ @PersistenceContext(unitName = "db2")
+ private EntityManager em2;
+
+ @PersistenceContext(unitName = "db3")
+ private EntityManager em3;
+
+ @PostConstruct
+ @TransactionAttribute(TransactionAttributeType.SUPPORTS)
+ public void initDatabase() {
+ em1.find(Person.class, 0);
+ em2.find(Person.class, 0);
+ em3.find(Person.class, 0);
+ }
+}
Propchange: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/BoostrapUtility.java
------------------------------------------------------------------------------
svn:executable = *
Added: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/DeterminedRouter.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/DeterminedRouter.java?rev=1138889&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/DeterminedRouter.java (added)
+++ openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/DeterminedRouter.java Thu Jun 23 14:10:18 2011
@@ -0,0 +1,109 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.dynamicdatasourcerouting;
+
+import org.apache.openejb.resource.jdbc.AbstractRouter;
+
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * @author Romain Manni-Bucau
+ */
+public class DeterminedRouter extends AbstractRouter {
+ private String dataSourceNames;
+ private String defaultDataSourceName;
+ private Map<String, DataSource> dataSources = null;
+ private ThreadLocal<DataSource> currentDataSource = new ThreadLocal<DataSource>();
+
+ /**
+ * @param datasourceList datasource resource name, separator is a space
+ */
+ public void setDataSourceNames(String datasourceList) {
+ dataSourceNames = datasourceList;
+ }
+
+ /**
+ * lookup datasource in openejb resources
+ */
+ private void init() {
+ dataSources = new ConcurrentHashMap<String, DataSource>();
+ for (String ds : dataSourceNames.split(" ")) {
+ try {
+ Object o = getOpenEJBResource(ds);
+ if (o instanceof DataSource) {
+ dataSources.put(ds, DataSource.class.cast(o));
+ }
+ } catch (NamingException e) {
+ // ignored
+ }
+ }
+ }
+
+ /**
+ * @return the user selected data source if it is set
+ * or the default one
+ * @throws IllegalArgumentException if the data source is not found
+ */
+ @Override public DataSource getDataSource() {
+ // lazy init of routed datasources
+ if (dataSources == null) {
+ init();
+ }
+
+ // if no datasource is selected use the default one
+ if (currentDataSource.get() == null) {
+ if (dataSources.containsKey(defaultDataSourceName)) {
+ return dataSources.get(defaultDataSourceName);
+
+ } else {
+ throw new IllegalArgumentException("you have to specify at least one datasource");
+ }
+ }
+
+ // the developper set the datasource to use
+ return currentDataSource.get();
+ }
+
+ /**
+ *
+ * @param datasourceName data source name
+ */
+ public void setDataSource(String datasourceName) {
+ if (dataSources == null) {
+ init();
+ }
+ if (!dataSources.containsKey(datasourceName)) {
+ throw new IllegalArgumentException("data source called " + datasourceName + " can't be found.");
+ }
+ DataSource ds = dataSources.get(datasourceName);
+ currentDataSource.set(ds);
+ }
+
+ /**
+ * reset the data source
+ */
+ public void clear() {
+ currentDataSource.remove();
+ }
+
+ public void setDefaultDataSourceName(String name) {
+ this.defaultDataSourceName = name;
+ }
+}
Propchange: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/DeterminedRouter.java
------------------------------------------------------------------------------
svn:executable = *
Added: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/Person.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/Person.java?rev=1138889&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/Person.java (added)
+++ openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/Person.java Thu Jun 23 14:10:18 2011
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.dynamicdatasourcerouting;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+/**
+ * @author Romain Manni-Bucau
+ */
+@Entity
+public class Person {
+ @Id
+ private long id;
+ private String name;
+
+ public Person() {
+ // no-op
+ }
+
+ public Person(int i, String n) {
+ id = i;
+ name = n;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Propchange: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/Person.java
------------------------------------------------------------------------------
svn:executable = *
Added: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/RoutedPersister.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/RoutedPersister.java?rev=1138889&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/RoutedPersister.java (added)
+++ openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/RoutedPersister.java Thu Jun 23 14:10:18 2011
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.dynamicdatasourcerouting;
+
+import javax.annotation.Resource;
+import javax.ejb.Stateless;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+/**
+ * @author Romain Manni-Bucau
+ */
+@Stateless
+public class RoutedPersister {
+ @PersistenceContext(unitName = "router")
+ private EntityManager em;
+
+ @Resource(name = "My Router", type = DeterminedRouter.class)
+ private DeterminedRouter router;
+
+ public void persist(int id, String name, String ds) {
+ router.setDataSource(ds);
+ em.persist(new Person(id, name));
+ }
+}
Propchange: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/java/org/superbiz/dynamicdatasourcerouting/RoutedPersister.java
------------------------------------------------------------------------------
svn:executable = *
Added: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/org.router/service-jar.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/org.router/service-jar.xml?rev=1138889&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/org.router/service-jar.xml (added)
+++ openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/org.router/service-jar.xml Thu Jun 23 14:10:18 2011
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<ServiceJar>
+ <ServiceProvider id="DeterminedRouter" service="Resource"
+ type="org.apache.openejb.resource.jdbc.Router"
+ class-name="org.superbiz.dynamicdatasourcerouting.DeterminedRouter">
+ DataSourceNames
+ DefaultDataSourceName
+ </ServiceProvider>
+</ServiceJar>
+
Propchange: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/org.router/service-jar.xml
------------------------------------------------------------------------------
svn:executable = *
Added: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/persistence.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/persistence.xml?rev=1138889&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/persistence.xml (added)
+++ openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/persistence.xml Thu Jun 23 14:10:18 2011
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<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="router" transaction-type="JTA">
+ <jta-data-source>Routed Datasource</jta-data-source>
+ <class>org.superbiz.dynamicdatasourcerouting.Person</class>
+ <properties>
+ <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+ </properties>
+ </persistence-unit>
+
+ <persistence-unit name="db1" transaction-type="JTA">
+ <jta-data-source>database1</jta-data-source>
+ <class>org.superbiz.dynamicdatasourcerouting.Person</class>
+ <properties>
+ <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+ </properties>
+ </persistence-unit>
+ <persistence-unit name="db2" transaction-type="JTA">
+ <jta-data-source>database2</jta-data-source>
+ <class>org.superbiz.dynamicdatasourcerouting.Person</class>
+ <properties>
+ <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+ </properties>
+ </persistence-unit>
+ <persistence-unit name="db3" transaction-type="JTA">
+ <jta-data-source>database3</jta-data-source>
+ <class>org.superbiz.dynamicdatasourcerouting.Person</class>
+ <properties>
+ <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+ </properties>
+ </persistence-unit>
+</persistence>
Propchange: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/main/resources/META-INF/persistence.xml
------------------------------------------------------------------------------
svn:executable = *
Added: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/test/java/org/superbiz/dynamicdatasourcerouting/DynamicDataSourceTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/test/java/org/superbiz/dynamicdatasourcerouting/DynamicDataSourceTest.java?rev=1138889&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/test/java/org/superbiz/dynamicdatasourcerouting/DynamicDataSourceTest.java (added)
+++ openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/test/java/org/superbiz/dynamicdatasourcerouting/DynamicDataSourceTest.java Thu Jun 23 14:10:18 2011
@@ -0,0 +1,157 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.dynamicdatasourcerouting;
+
+import org.apache.openejb.client.LocalInitialContextFactory;
+import org.apache.openejb.resource.jdbc.Router;
+import org.junit.Test;
+
+import javax.ejb.embeddable.EJBContainer;
+import javax.naming.Context;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * If you are using openejb.xml the test properties are:
+ *
+ *
+ <!-- Router and datasource -->
+ <Resource id="My Router" type="org.apache.openejb.router.test.DynamicDataSourceTest$DeterminedRouter" provider="org.routertest:DeterminedRouter">
+ DatasourceNames = database1 database2 database3
+ DefaultDataSourceName = database1
+ </Resource>
+ <Resource id="Routed Datasource" type="org.apache.openejb.resource.jdbc.Router" provider="org.router:RoutedDataSource">
+ Router = My Router
+ </Resource>
+
+ <!-- real datasources -->
+ <Resource id="database1" type="DataSource">
+ JdbcDriver = org.hsqldb.jdbcDriver
+ JdbcUrl = jdbc:hsqldb:mem:db1
+ UserName = sa
+ Password
+ JtaManaged = true
+ </Resource>
+ <Resource id="database2" type="DataSource">
+ JdbcDriver = org.hsqldb.jdbcDriver
+ JdbcUrl = jdbc:hsqldb:mem:db2
+ UserName = sa
+ Password
+ JtaManaged = true
+ </Resource>
+ <Resource id="database3" type="DataSource">
+ JdbcDriver = org.hsqldb.jdbcDriver
+ JdbcUrl = jdbc:hsqldb:mem:db3
+ UserName = sa
+ Password
+ JtaManaged = true
+ </Resource>
+ <!-- Router and datasource -->
+ <Resource id="My Router" type="org.apache.openejb.router.test.DynamicDataSourceTest$DeterminedRouter" provider="org.routertest:DeterminedRouter">
+ DatasourceNames = database1 database2 database3
+ DefaultDataSourceName = database1
+ </Resource>
+ <Resource id="Routed Datasource" type="org.apache.openejb.resource.jdbc.Router" provider="org.router:RoutedDataSource">
+ Router = My Router
+ </Resource>
+
+ <!-- real datasources -->
+ <Resource id="database1" type="DataSource">
+ JdbcDriver = org.hsqldb.jdbcDriver
+ JdbcUrl = jdbc:hsqldb:mem:db1
+ UserName = sa
+ Password
+ JtaManaged = true
+ </Resource>
+ <Resource id="database2" type="DataSource">
+ JdbcDriver = org.hsqldb.jdbcDriver
+ JdbcUrl = jdbc:hsqldb:mem:db2
+ UserName = sa
+ Password
+ JtaManaged = true
+ </Resource>
+ <Resource id="database3" type="DataSource">
+ JdbcDriver = org.hsqldb.jdbcDriver
+ JdbcUrl = jdbc:hsqldb:mem:db3
+ UserName = sa
+ Password
+ JtaManaged = true
+ </Resource>
+
+ * @author Romain Manni-Bucau
+ */
+public class DynamicDataSourceTest {
+ @Test public void route() throws Exception {
+ String[] databases = new String[] { "database1", "database2", "database3" };
+
+ Properties properties = new Properties();
+ properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
+
+ // resources
+ // datasources
+ for (int i = 1; i <= databases.length; i++) {
+ String dbName = databases[i - 1];
+ properties.setProperty(dbName, "new://Resource?type=DataSource");
+ dbName += ".";
+ properties.setProperty(dbName + "JdbcDriver", "org.hsqldb.jdbcDriver");
+ properties.setProperty(dbName + "JdbcDriver", "org.hsqldb.jdbcDriver");
+ properties.setProperty(dbName + "JdbcUrl", "jdbc:hsqldb:mem:db" + i);
+ properties.setProperty(dbName + "UserName", "sa");
+ properties.setProperty(dbName + "Password", "");
+ properties.setProperty(dbName + "JtaManaged", "true");
+ }
+
+ // router
+ properties.setProperty("My Router", "new://Resource?provider=org.router:DeterminedRouter&type=" + DeterminedRouter.class.getName());
+ properties.setProperty("My Router.DatasourceNames", "database1 database2 database3");
+ properties.setProperty("My Router.DefaultDataSourceName", "database1");
+
+ // routed datasource
+ properties.setProperty("Routed Datasource", "new://Resource?provider=RoutedDataSource&type=" + Router.class.getName());
+ properties.setProperty("Routed Datasource.Router", "My Router");
+
+ // convenient bean to create tables for each persistence unit
+ /*BoostrapUtility utility = (BoostrapUtility) ctx.lookup("java:global/dynamic-datasource-routing/BoostrapUtility");
+ utility.initDatabase();*/
+
+ Context ctx = EJBContainer.createEJBContainer(properties).getContext();
+ RoutedPersister ejb = (RoutedPersister) ctx.lookup("java:global/dynamic-datasource-routing/RoutedPersister");
+ for (int i = 0; i < 18; i++) {
+ // persisting a person on database db -> kind of manual round robin
+ String name = "record " + i;
+ String db = databases[i % 3];
+ ejb.persist(i, name, db);
+ }
+
+ // assert database records number using jdbc
+ for (int i = 1; i <= databases.length; i++) {
+ Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:db" + i, "sa", "");
+ Statement st = connection.createStatement();
+ ResultSet rs = st.executeQuery("select count(*) from PERSON");
+ rs.next();
+ assertEquals(6, rs.getInt(1));
+ st.close();
+ connection.close();
+ }
+ }
+}
+
Propchange: openejb/trunk/openejb3/examples/dynamic-datasource-routing/src/test/java/org/superbiz/dynamicdatasourcerouting/DynamicDataSourceTest.java
------------------------------------------------------------------------------
svn:executable = *
Modified: openejb/trunk/openejb3/examples/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/pom.xml?rev=1138889&r1=1138888&r2=1138889&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/pom.xml (original)
+++ openejb/trunk/openejb3/examples/pom.xml Thu Jun 23 14:10:18 2011
@@ -77,6 +77,7 @@
<module>schedule-methods-meta</module>
<module>testing-security-meta</module>
<module>decorators</module>
+ <module>dynamic-datasource-routing</module>
</modules>
<profiles>
<profile>