You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2019/06/24 21:56:29 UTC
[tomee] branch tomee-1.7.x updated: Backported from master
This is an automated email from the ASF dual-hosted git repository.
jgallimore pushed a commit to branch tomee-1.7.x
in repository https://gitbox.apache.org/repos/asf/tomee.git
The following commit(s) were added to refs/heads/tomee-1.7.x by this push:
new 023aad5 Backported from master
new 5254102 Merge pull request #489 from jgallimore/xa-ds-grinder-17x
023aad5 is described below
commit 023aad5f85fd6ec37743c0ba5a463b3c37237600
Author: Jonathan Gallimore <jo...@jrg.me.uk>
AuthorDate: Tue Jun 18 14:05:59 2019 +0100
Backported from master
---
examples/xa-datasource/.gitignore | 3 +
examples/xa-datasource/README.md | 114 +++++++
examples/xa-datasource/grinder.sh | 28 ++
examples/xa-datasource/pom.xml | 331 ++++++++++++++++++++-
examples/xa-datasource/src/assembly/tests.xml | 32 ++
.../java/org/superbiz/injection/jpa/Movie.java | 11 +
.../java/org/superbiz/injection/jpa/Movies.java | 9 +-
.../org/superbiz/injection/jpa/MoviesDirect.java | 45 ++-
.../java/org/superbiz/injection/jpa/MoviesXA.java | 4 +-
.../main/java/org/superbiz/rest/MovieService.java | 110 +++++++
.../src/main/resources/META-INF/persistence.xml | 1 +
.../src/main/webapp/WEB-INF/resources.xml | 45 +++
12 files changed, 718 insertions(+), 15 deletions(-)
diff --git a/examples/xa-datasource/.gitignore b/examples/xa-datasource/.gitignore
new file mode 100644
index 0000000..d98b647
--- /dev/null
+++ b/examples/xa-datasource/.gitignore
@@ -0,0 +1,3 @@
+test/
+grinder/
+
diff --git a/examples/xa-datasource/README.md b/examples/xa-datasource/README.md
index 5128f0c..3509b3a 100644
--- a/examples/xa-datasource/README.md
+++ b/examples/xa-datasource/README.md
@@ -215,3 +215,117 @@ When we run our test case we should see output similar to the following.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
+## Performance testing
+
+### Prerequisites
+
+
+- Oralce DB
+
+ * Update your maven `setings.xml` file to contains in the `<servers>` section the following entry:
+
+ ```xml
+ <server>
+ <id>maven.oracle.com </id>
+ <username>YourOracleAccountUsername</username>
+ <password>YourOracleAccountPassword</password>
+ <configuration>
+ <basicAuthScope>
+ <host>ANY </host>
+ <port>ANY </port>
+ <realm>OAM 11g </realm>
+ </basicAuthScope>
+ <httpConfiguration>
+ <all>
+ <params>
+ <property>
+ <name>http.protocol.allow-circular-redirects </name>
+ <value>%b,true </value>
+ </property>
+ </params>
+ </all>
+ </httpConfiguration>
+ </configuration>
+ </server>
+ ```
+
+ * Update file `pom.xml`, section `<dependency>` with the following:
+
+ ```xml
+ <dependency>
+ <groupId>com.oracle.jdbc</groupId>
+ <artifactId>ojdbc8</artifactId>
+ <version>18.3.0.0</version>
+ <scope>provided</scope>
+ </dependency>
+ ```
+
+ * Update file `pom.xml`, section `<repositories>` with the following:
+
+ ```xml
+ <repository>
+ <id>maven.oracle.com</id>
+ <name>oracle-maven-repo</name>
+ <url>https://maven.oracle.com</url>
+ <layout>default</layout>
+ <releases>
+ <enabled>true</enabled>
+ <updatePolicy>always</updatePolicy>
+ </releases>
+ </repository>
+ ```
+
+ * Update file `pom.xml`, after `</repositories>` section, add the following:
+
+ ```xml
+ <pluginRepositories>
+ <pluginRepository>
+ <id>maven.oracle.com</id>
+ <name>oracle-maven-repo</name>
+ <url>https://maven.oracle.com</url>
+ <layout>default</layout>
+ <releases>
+ <enabled>true</enabled>
+ <updatePolicy>always</updatePolicy>
+ </releases>
+ </pluginRepository>
+ </pluginRepositories>
+ ```
+
+ * Update file `pom.xml`, add the type of JDBC jar for the tomee-maven-plugin:
+
+ ```xml
+ <plugin>
+ <groupId>org.apache.tomee.maven</groupId>
+ <artifactId>tomee-maven-plugin</artifactId>
+ <version>${tomee.version}</version>
+ <configuration>
+ <tomeeVersion>${tomee.version}</tomeeVersion>
+ <tomeeClassifier>plume</tomeeClassifier>
+ <tomeeHttpPort>9080</tomeeHttpPort>
+ <tomeeShutdownPort>9005</tomeeShutdownPort>
+ <libs>
+ <lib>com.oracle.jdbc:ojdbc8:18.3.0.0</lib>
+ </libs>
+ </configuration>
+ </plugin>
+ ```
+
+
+ For further Oracle JDBC Maven setup you can check [Oracle Article](https://blogs.oracle.com/dev2dev/get-oracle-jdbc-drivers-and-ucp-from-oracle-maven-repository-without-ides)
+
+
+### Run the application
+From a terminal
+
+ mvn clean install tomee:run
+
+### Run Grinder console
+On a new terminal execute:
+
+ ./grinder.sh
+
+ Once the console UI is available, press the button `Start the worker processes`
+
+ The load test will continue until you press on the console the button `Stop the worker processes and the agent processes`
+
\ No newline at end of file
diff --git a/examples/xa-datasource/grinder.sh b/examples/xa-datasource/grinder.sh
new file mode 100755
index 0000000..4f18701
--- /dev/null
+++ b/examples/xa-datasource/grinder.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+#
+# Prior to running this script, start TomEE with:
+# mvn clean install tomee:run
+#
+TESTS=$(ls $PWD/target/*-tests.jar)
+PROPERTIES=$PWD/target/grinder.properties
+echo "
+grinder.script $PWD/target/test-classes/grinder.py
+grinder.processes 1
+grinder.threads 2
+grinder.runs 0
+grinder.jvm.classpath $TESTS
+grinder.logDirectory $PWD/target/logs
+grinder.numberOfOldLogs 0
+" > $PROPERTIES
+
+
+(
+cd $(dirname "$0")/grinder/
+java -cp grinder.jar net.grinder.Console &
+sleep 10
+)
+(
+cd $(dirname "$0")/grinder/
+java -cp grinder.jar net.grinder.Grinder $PROPERTIES
+)
\ No newline at end of file
diff --git a/examples/xa-datasource/pom.xml b/examples/xa-datasource/pom.xml
index ce1e8bd..57ef6d0 100644
--- a/examples/xa-datasource/pom.xml
+++ b/examples/xa-datasource/pom.xml
@@ -24,14 +24,16 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.superbiz</groupId>
<artifactId>xa-datasource</artifactId>
- <packaging>jar</packaging>
+ <packaging>war</packaging>
<version>1.1.0-SNAPSHOT</version>
<name>OpenEJB :: Examples :: XA Datasource configuration and usage</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<derby.version>10.12.1.1</derby.version>
+ <tomee.version>1.7.6-SNAPSHOT</tomee.version>
</properties>
<build>
+ <finalName>dbtest</finalName>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
@@ -39,8 +41,313 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
- <source>1.7</source>
- <target>1.7</target>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-war-plugin</artifactId>
+ <configuration>
+ <failOnMissingWebXml>false</failOnMissingWebXml>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <executions>
+ <execution>
+ <id>make-assembly</id>
+ <!-- this is used for inheritance merges -->
+ <phase>package</phase>
+ <!-- bind to the packaging phase -->
+ <goals>
+ <goal>single</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <descriptors>
+ <descriptor>src/assembly/tests.xml</descriptor>
+ </descriptors>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <version>2.10</version>
+ <executions>
+ <execution>
+ <id>copy-grinder-libs</id>
+ <phase>process-test-resources</phase>
+ <configuration>
+ <artifactItems>
+ <artifactItem>
+ <groupId>net.sf.grinder</groupId>
+ <artifactId>grinder</artifactId>
+ <version>3.11</version>
+ <destFileName>grinder.jar</destFileName>
+ </artifactItem>
+ <artifactItem>
+ <groupId>net.sf.grinder</groupId>
+ <artifactId>grinder-dcr-agent</artifactId>
+ <version>3.11</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>net.sf.grinder</groupId>
+ <artifactId>grinder-http</artifactId>
+ <version>3.11</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>net.sf.grinder</groupId>
+ <artifactId>grinder-core</artifactId>
+ <version>3.11</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.6.4</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-jdk14</artifactId>
+ <version>1.6.4</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>asm</groupId>
+ <artifactId>asm</artifactId>
+ <version>3.2</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.picocontainer</groupId>
+ <artifactId>picocontainer</artifactId>
+ <version>2.13.6</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.python</groupId>
+ <artifactId>jython-standalone</artifactId>
+ <version>2.5.3</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>1.0.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-core</artifactId>
+ <version>1.0.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>net.sf.grinder</groupId>
+ <artifactId>grinder-httpclient</artifactId>
+ <version>3.11</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>net.sf.grinder</groupId>
+ <artifactId>grinder-xmlbeans</artifactId>
+ <version>3.11</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>javax.xml</groupId>
+ <artifactId>jsr173</artifactId>
+ <version>1.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.apache.xmlbeans</groupId>
+ <artifactId>xmlbeans</artifactId>
+ <version>2.5.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>stax</groupId>
+ <artifactId>stax-api</artifactId>
+ <version>1.0.1</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>net.sf.grinder</groupId>
+ <artifactId>grinder-console-service</artifactId>
+ <version>3.11</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.clojure</groupId>
+ <artifactId>clojure</artifactId>
+ <version>1.4.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.clojure</groupId>
+ <artifactId>tools.logging</artifactId>
+ <version>0.2.3</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>ring</groupId>
+ <artifactId>ring-core</artifactId>
+ <version>1.1.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.1</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ <version>1.6</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>commons-fileupload</groupId>
+ <artifactId>commons-fileupload</artifactId>
+ <version>1.2.1</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.5</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>clj-time</groupId>
+ <artifactId>clj-time</artifactId>
+ <version>0.3.7</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>joda-time</groupId>
+ <artifactId>joda-time</artifactId>
+ <version>2.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>ring</groupId>
+ <artifactId>ring-jetty-adapter</artifactId>
+ <version>1.1.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>ring</groupId>
+ <artifactId>ring-servlet</artifactId>
+ <version>1.1.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-server</artifactId>
+ <version>7.6.1.v20120215</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.eclipse.jetty.orbit</groupId>
+ <artifactId>javax.servlet</artifactId>
+ <version>2.5.0.v201103041518</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-continuation</artifactId>
+ <version>7.6.1.v20120215</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-http</artifactId>
+ <version>7.6.1.v20120215</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-io</artifactId>
+ <version>7.6.1.v20120215</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-util</artifactId>
+ <version>7.6.1.v20120215</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>ring-middleware-format</groupId>
+ <artifactId>ring-middleware-format</artifactId>
+ <version>0.2.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.clojure</groupId>
+ <artifactId>core.memoize</artifactId>
+ <version>0.5.1</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.clojure</groupId>
+ <artifactId>core.cache</artifactId>
+ <version>0.5.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>cheshire</groupId>
+ <artifactId>cheshire</artifactId>
+ <version>4.0.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-core</artifactId>
+ <version>2.0.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>com.fasterxml.jackson.dataformat</groupId>
+ <artifactId>jackson-dataformat-smile</artifactId>
+ <version>2.0.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>clj-yaml</groupId>
+ <artifactId>clj-yaml</artifactId>
+ <version>0.3.1</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.yaml</groupId>
+ <artifactId>snakeyaml</artifactId>
+ <version>1.5</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>compojure</groupId>
+ <artifactId>compojure</artifactId>
+ <version>1.0.4</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.clojure</groupId>
+ <artifactId>core.incubator</artifactId>
+ <version>0.1.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>org.clojure</groupId>
+ <artifactId>tools.macro</artifactId>
+ <version>0.1.0</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>clout</groupId>
+ <artifactId>clout</artifactId>
+ <version>1.0.1</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>clj-stacktrace</groupId>
+ <artifactId>clj-stacktrace</artifactId>
+ <version>0.2.4</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>net.sf.grinder</groupId>
+ <artifactId>grinder-swing-console</artifactId>
+ <version>3.11</version>
+ </artifactItem>
+ <artifactItem>
+ <groupId>net.sf.jedit-syntax</groupId>
+ <artifactId>jedit-syntax</artifactId>
+ <version>2.2.2</version>
+ </artifactItem>
+ </artifactItems>
+ <outputDirectory>
+ ${project.basedir}/grinder
+ </outputDirectory>
+ </configuration>
+ <goals>
+ <goal>copy</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.openejb.maven</groupId>
+ <artifactId>tomee-maven-plugin</artifactId>
+ <version>${tomee.version}</version>
+ <configuration>
+ <tomeeVersion>${tomee.version}</tomeeVersion>
+ <tomeeClassifier>jaxrs</tomeeClassifier>
+ <tomeeHttpPort>9080</tomeeHttpPort>
+ <tomeeShutdownPort>9005</tomeeShutdownPort>
</configuration>
</plugin>
</plugins>
@@ -51,6 +358,11 @@
<name>Apache Snapshot Repository</name>
<url>https://repository.apache.org/content/groups/snapshots</url>
</repository>
+ <repository>
+ <id>Clojars</id>
+ <name>Clojars Repository</name>
+ <url>http://clojars.org/repo/</url>
+ </repository>
</repositories>
<dependencies>
<dependency>
@@ -94,7 +406,20 @@
<version>${derby.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.tomitribe</groupId>
+ <artifactId>tomitribe-util</artifactId>
+ <version>1.1.0</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>fluent-hc</artifactId>
+ <version>4.5.7</version>
+ <scope>test</scope>
+ </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:
diff --git a/examples/xa-datasource/src/assembly/tests.xml b/examples/xa-datasource/src/assembly/tests.xml
new file mode 100644
index 0000000..df628dc
--- /dev/null
+++ b/examples/xa-datasource/src/assembly/tests.xml
@@ -0,0 +1,32 @@
+<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
+ <!-- TODO: a jarjar format would be better -->
+ <id>tests</id>
+ <formats>
+ <format>jar</format>
+ </formats>
+ <includeBaseDirectory>false</includeBaseDirectory>
+ <dependencySets>
+ <dependencySet>
+ <outputDirectory>/</outputDirectory>
+ <useProjectArtifact>true</useProjectArtifact>
+ <unpack>true</unpack>
+ <scope>test</scope>
+ <excludes>
+ <exclude>org.apache.cxf*:*</exclude>
+ <exclude>org.apache.openejb*:*</exclude>
+ <exclude>org.apache.openjpa*:*</exclude>
+ <exclude>org.apache.openwebbeans*:*</exclude>
+ <exclude>org.apache.xbean*:*</exclude>
+ <exclude>org.apache.myfaces*:*</exclude>
+ <exclude>org.apache.tomcat*:*</exclude>
+ <exclude>org.hsqldb:*</exclude>
+ <exclude>org.jboss.*:*</exclude>
+ <exclude>org.eclipse*:*</exclude>
+ <exclude>org.codehaus*:*</exclude>
+ <exclude>com.sun*:*</exclude>
+ </excludes>
+ </dependencySet>
+ </dependencySets>
+</assembly>
diff --git a/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/Movie.java b/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/Movie.java
index 1bc65eb..80793c0 100644
--- a/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/Movie.java
+++ b/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/Movie.java
@@ -20,16 +20,27 @@ package org.superbiz.injection.jpa;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
@Entity
+@XmlAccessorType(XmlAccessType.NONE)
+@XmlRootElement
public class Movie {
@Id
@GeneratedValue
private long id;
+ @XmlElement
private String director;
+
+ @XmlElement
private String title;
+
+ @XmlElement
private int year;
public Movie() {
diff --git a/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/Movies.java b/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/Movies.java
index 4e136aa..e107778 100644
--- a/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/Movies.java
+++ b/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/Movies.java
@@ -41,14 +41,19 @@ public class Movies {
}
public List<Movie> getMovies() throws Exception {
- Query query = entityManager.createQuery("SELECT m from Movie as m");
+ final Query query = entityManager.createQuery("SELECT m from Movie as m");
return query.getResultList();
}
public void deleteAll() throws Exception {
- Query query = entityManager.createQuery("DELETE from Movie");
+ final Query query = entityManager.createQuery("DELETE from Movie");
query.executeUpdate();
}
+ public long count() throws Exception {
+ final Query query = entityManager.createQuery("select count(m) from Movie as m");
+ return (Long) query.getSingleResult();
+ }
+
}
//END SNIPPET: code
diff --git a/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/MoviesDirect.java b/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/MoviesDirect.java
index 22646b0..7780604 100644
--- a/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/MoviesDirect.java
+++ b/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/MoviesDirect.java
@@ -22,6 +22,7 @@ import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
+import java.sql.SQLException;
@Singleton
public class MoviesDirect {
@@ -30,18 +31,44 @@ public class MoviesDirect {
private DataSource ds;
public int count() {
- try (final Connection connection = ds.getConnection()) {
- try (final PreparedStatement ps = connection.prepareStatement("select count(1) from movie")) {
- try (final ResultSet rs = ps.executeQuery()) {
- if (rs != null && rs.next()) {
- return rs.getInt(1);
- } else {
- return 0;
- }
- }
+ Connection connection = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+
+ try {
+ connection = ds.getConnection();
+ ps = connection.prepareStatement("select count(1) from movie");
+ rs = ps.executeQuery();
+
+ if (rs != null && rs.next()) {
+ return rs.getInt(1);
+ } else {
+ return 0;
}
} catch (final Exception e) {
throw new RuntimeException("Unable to execute query against the database");
+ } finally {
+ if (rs != null) {
+ try {
+ rs.close();
+ } catch (SQLException e) {
+ // ignore
+ }
+ }
+ if (ps != null) {
+ try {
+ ps.close();
+ } catch (SQLException e) {
+ // ignore
+ }
+ }
+ if (connection != null) {
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ // ignore
+ }
+ }
}
}
}
diff --git a/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/MoviesXA.java b/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/MoviesXA.java
index 05a384d..f139eac 100644
--- a/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/MoviesXA.java
+++ b/examples/xa-datasource/src/main/java/org/superbiz/injection/jpa/MoviesXA.java
@@ -165,7 +165,9 @@ public class MoviesXA {
state = XA_STATE_STARTED;
}
});
- } catch (final RollbackException | SystemException e) {
+ } catch (final RollbackException e) {
+ throw new IllegalStateException(e);
+ } catch (final SystemException e) {
throw new IllegalStateException(e);
}
}
diff --git a/examples/xa-datasource/src/main/java/org/superbiz/rest/MovieService.java b/examples/xa-datasource/src/main/java/org/superbiz/rest/MovieService.java
new file mode 100644
index 0000000..a58242d
--- /dev/null
+++ b/examples/xa-datasource/src/main/java/org/superbiz/rest/MovieService.java
@@ -0,0 +1,110 @@
+package org.superbiz.rest;
+
+import org.superbiz.injection.jpa.Movie;
+import org.superbiz.injection.jpa.Movies;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.ejb.Lock;
+import javax.ejb.Singleton;
+import javax.ejb.Startup;
+import javax.sql.DataSource;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.List;
+import java.util.logging.Logger;
+
+import static javax.ejb.LockType.READ;
+
+@Lock(READ)
+@Singleton
+@Path("/")
+@Startup
+public class MovieService {
+
+ private final static Logger LOGGER = Logger.getLogger(MovieService.class.getName());
+
+ @Resource
+ private DataSource ds;
+
+ @EJB
+ private Movies movies;
+
+ @GET
+ @Path("setup")
+ public void setup() throws Exception {
+ final long count = movies.count();
+
+ if (count == 0) {
+ movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
+ movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
+ movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));
+ }
+ }
+
+ @GET
+ public Response checkDatabase() {
+ Connection connection = null;
+ PreparedStatement ps = null;
+ ResultSet rs = null;
+
+ try {
+ connection = ds.getConnection();
+ ps = connection.prepareStatement("select count(1) from movie");
+ rs = ps.executeQuery();
+
+ int results = 0;
+
+ while (rs.next()) {
+ results++;
+ }
+
+ LOGGER.info("Request checkDatabase() | result [" + results + "]");
+ return Response.ok().entity(String.valueOf(results)).build();
+ } catch (final Exception e) {
+ throw new WebApplicationException(e, Response.serverError().build());
+ } finally {
+ if (rs != null) {
+ try {
+ rs.close();
+ } catch (SQLException e) {
+ // ignore
+ }
+ }
+ if (ps != null) {
+ try {
+ ps.close();
+ } catch (SQLException e) {
+ // ignore
+ }
+ }
+ if (connection != null) {
+ try {
+ connection.close();
+ } catch (SQLException e) {
+ // ignore
+ }
+ }
+ }
+ }
+
+ @GET
+ @Path("list")
+ @Produces(MediaType.APPLICATION_JSON)
+ public List<Movie> fetchAll() throws Exception {
+ return movies.getMovies();
+ }
+
+
+}
+
+
diff --git a/examples/xa-datasource/src/main/resources/META-INF/persistence.xml b/examples/xa-datasource/src/main/resources/META-INF/persistence.xml
index 798f6a9..3936aa6 100644
--- a/examples/xa-datasource/src/main/resources/META-INF/persistence.xml
+++ b/examples/xa-datasource/src/main/resources/META-INF/persistence.xml
@@ -26,6 +26,7 @@
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+ <property name="openjpa.Log" value="DefaultLevel=INFO, MetaData=TRACE, SQL=TRACE"/>
</properties>
</persistence-unit>
</persistence>
diff --git a/examples/xa-datasource/src/main/webapp/WEB-INF/resources.xml b/examples/xa-datasource/src/main/webapp/WEB-INF/resources.xml
new file mode 100644
index 0000000..c4bff3d
--- /dev/null
+++ b/examples/xa-datasource/src/main/webapp/WEB-INF/resources.xml
@@ -0,0 +1,45 @@
+<resources>
+ <Resource id="movieDatabaseXA" type="javax.sql.XADataSource" class-name="org.apache.derby.jdbc.EmbeddedXADataSource">
+ DatabaseName=testdb
+ CreateDatabase=create
+ </Resource>
+ <Resource id="movieDatabase" type="DataSource">
+ XaDataSource=movieDatabaseXA
+ UserName=admin
+ Password=admin
+ ValidationQuery=values 1
+ InitialSize=2
+ MaxActive=128
+ MaxIdle=25
+ MinIdle=10
+ TestWhileIdle=true
+ TestOnBorrow=false
+ TestOnReturn=false
+ AccessToUnderlyingConnectionAllowed=true
+ TimeBetweenEvictionRuns=1 minute
+ MaxWaitTime=0 seconds
+ JtaManaged=true
+ PoolPreparedStatements=true
+ MaxOpenPreparedStatements=1024
+ </Resource>
+ <Resource id="movieDatabaseUnmanaged" type="DataSource">
+ JdbcDriver org.apache.derby.jdbc.EmbeddedDriver
+ JdbcUrl jdbc:derby:testdb;create=true
+ UserName admin
+ Password admin
+ ValidationQuery=values 1
+ InitialSize=2
+ MaxActive=128
+ MaxIdle=25
+ MinIdle=10
+ TestWhileIdle=true
+ TestOnBorrow=false
+ TestOnReturn=false
+ AccessToUnderlyingConnectionAllowed=true
+ TimeBetweenEvictionRuns=1 minute
+ MaxWaitTime=0 seconds
+ JtaManaged=false
+ PoolPreparedStatements=true
+ MaxOpenPreparedStatements=1024
+ </Resource>
+</resources>