From f17ff8ba3526535c9f1ca0c4f44d425f7b357e4f Mon Sep 17 00:00:00 2001 From: GHajba Date: Wed, 15 Oct 2014 15:39:29 +0200 Subject: [PATCH 1/2] creating the interceptor example --- example/pom.xml | 594 +++++++++--------- .../javaee/example/BigListInterceptor.java | 86 +++ .../dev/javaee/example/CollectionUtils.java | 43 ++ .../dev/javaee/example/Ora01795Repo.java | 50 ++ .../hahamo/dev/javaee/example/TestEntity.java | 22 + .../src/main/webapp/WEB-INF/example-ds.xml | 26 +- 6 files changed, 494 insertions(+), 327 deletions(-) create mode 100755 example/src/main/java/biz/hahamo/dev/javaee/example/BigListInterceptor.java create mode 100755 example/src/main/java/biz/hahamo/dev/javaee/example/CollectionUtils.java create mode 100755 example/src/main/java/biz/hahamo/dev/javaee/example/Ora01795Repo.java create mode 100755 example/src/main/java/biz/hahamo/dev/javaee/example/TestEntity.java diff --git a/example/pom.xml b/example/pom.xml index ec2520d..9180002 100644 --- a/example/pom.xml +++ b/example/pom.xml @@ -1,314 +1,284 @@ - - - 4.0.0 - - biz.hahamo.dev.enterprise - example - 0.1-SNAPSHOT - war - GHajba's Java EE example project - A starter Java EE 7 webapp project for use on JBoss WildFly / WildFly, generated from the jboss-javaee6-webapp archetype and adapted by GHajba - - http://wildfly.org - - - Apache License, Version 2.0 - repo - http://www.apache.org/licenses/LICENSE-2.0.html - - - - - - - UTF-8 - - - 1.0.2.Final - - - 8.1.0.Final - 8.1.0.Final - - - 3.1 - 2.16 - 2.1.1 - - - 1.8 - 1.8 - - - - - - - - org.wildfly.bom - jboss-javaee-7.0-with-tools - ${version.jboss.bom} - pom - import - - - org.wildfly.bom - jboss-javaee-7.0-with-hibernate - ${version.jboss.bom} - pom - import - - - - - - - - - - - javax.enterprise - cdi-api - provided - - - - - org.jboss.spec.javax.annotation - jboss-annotations-api_1.2_spec - provided - - - - - org.jboss.resteasy - jaxrs-api - provided - - - - - org.hibernate.javax.persistence - hibernate-jpa-2.1-api - provided - - - - - org.jboss.spec.javax.ejb - jboss-ejb-api_3.2_spec - provided - - - - - - - org.hibernate - hibernate-validator - provided - - - org.slf4j - slf4j-api - - - - - - - org.jboss.spec.javax.faces - jboss-jsf-api_2.2_spec - provided - - - - - - - org.hibernate - hibernate-jpamodelgen - provided - - - - - org.hibernate - hibernate-validator-annotation-processor - provided - - - - - junit - junit - test - - - - - - org.jboss.arquillian.junit - arquillian-junit-container - test - - - - org.jboss.arquillian.protocol - arquillian-protocol-servlet - test - - - - - - - ${project.artifactId} - - - - maven-compiler-plugin - ${version.compiler.plugin} - - ${maven.compiler.source} - ${maven.compiler.target} - - - - maven-war-plugin - ${version.war.plugin} - - - false - - - - - - org.wildfly.plugins - wildfly-maven-plugin - ${version.wildfly.maven.plugin} - - - - - - - - - default - - true - - - - - maven-surefire-plugin - ${version.surefire.plugin} - - true - - - - - - - - - - - - arq-wildfly-managed - - - org.wildfly - wildfly-arquillian-container-managed - ${version.arquillian.container} - test - - - - - - - - arq-wildfly-remote - - - org.wildfly - wildfly-arquillian-container-remote - ${version.arquillian.container} - test - - - - - - - - - - openshift - - - - maven-war-plugin - ${version.war.plugin} - - deployments - ROOT - - - - - - - + + + 4.0.0 + + biz.hahamo.dev.enterprise + example + 0.1-SNAPSHOT + war + GHajba's Java EE example project + A starter Java EE 7 webapp project for use on JBoss WildFly / WildFly, generated from the jboss-javaee6-webapp archetype and adapted by GHajba + + http://wildfly.org + + + Apache License, Version 2.0 + repo + http://www.apache.org/licenses/LICENSE-2.0.html + + + + + + + UTF-8 + + + 1.0.2.Final + + + 8.1.0.Final + 8.1.0.Final + + + 3.1 + 2.16 + 2.1.1 + + + 1.7 + 1.7 + + + + + + + + org.wildfly.bom + jboss-javaee-7.0-with-tools + ${version.jboss.bom} + pom + import + + + org.wildfly.bom + jboss-javaee-7.0-with-hibernate + ${version.jboss.bom} + pom + import + + + javax + javaee-api + 7.0 + provided + + + + + + + + + + + javax.enterprise + cdi-api + provided + + + + + org.jboss.spec.javax.annotation + jboss-annotations-api_1.2_spec + provided + + + + + org.jboss.resteasy + jaxrs-api + provided + + + + + org.hibernate.javax.persistence + hibernate-jpa-2.1-api + provided + + + + + org.jboss.spec.javax.ejb + jboss-ejb-api_3.2_spec + provided + + + + + + + org.hibernate + hibernate-validator + provided + + + org.slf4j + slf4j-api + + + + + + + org.jboss.spec.javax.faces + jboss-jsf-api_2.2_spec + provided + + + + + + + org.hibernate + hibernate-jpamodelgen + provided + + + + + org.hibernate + hibernate-validator-annotation-processor + provided + + + + + junit + junit + test + + + + + + org.jboss.arquillian.junit + arquillian-junit-container + test + + + + org.jboss.arquillian.protocol + arquillian-protocol-servlet + test + + + + javax + javaee-api + provided + + + + + + + ${project.artifactId} + + + + maven-compiler-plugin + ${version.compiler.plugin} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + maven-war-plugin + ${version.war.plugin} + + + false + + + + + + org.wildfly.plugins + wildfly-maven-plugin + ${version.wildfly.maven.plugin} + + + + + + + + + default + + true + + + + + maven-surefire-plugin + ${version.surefire.plugin} + + true + + + + + + + + + + + + arq-wildfly-managed + + + org.wildfly + wildfly-arquillian-container-managed + ${version.arquillian.container} + test + + + + + + + + arq-wildfly-remote + + + org.wildfly + wildfly-arquillian-container-remote + ${version.arquillian.container} + test + + + + + + + + + + openshift + + + + maven-war-plugin + ${version.war.plugin} + + deployments + ROOT + + + + + + + diff --git a/example/src/main/java/biz/hahamo/dev/javaee/example/BigListInterceptor.java b/example/src/main/java/biz/hahamo/dev/javaee/example/BigListInterceptor.java new file mode 100755 index 0000000..d770793 --- /dev/null +++ b/example/src/main/java/biz/hahamo/dev/javaee/example/BigListInterceptor.java @@ -0,0 +1,86 @@ +package biz.hahamo.dev.javaee.example; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import javax.interceptor.AroundInvoke; +import javax.interceptor.InvocationContext; + +/** + * Interceptor to handle big list database calling. + * + * @author GHajba + * + */ +@SuppressWarnings({ "unchecked", "rawtypes" }) +public class BigListInterceptor { + + private static final int MAX_LIST_SIZE = 1000; + + @AroundInvoke + public Object interceptBigList(final InvocationContext ctx) throws Exception { + + Object[] parameters = ctx.getParameters(); + + for (int i = 0; i < parameters.length; i++) { + if (!(parameters[i] instanceof Collection)) { + continue; + } + + List source = new ArrayList((Collection) parameters[i]); + + if (isNoBigList(source)) { + continue; + } + + List> listOfLists = CollectionUtils.splitLongList(source, MAX_LIST_SIZE); + + return callMethodForSmallLists(ctx, parameters, i, listOfLists, source.size()); + } + + return ctx.proceed(); + } + + private Object callMethodForSmallLists(final InvocationContext ctx, final Object[] parameters, final int i, + final List> listOfLists, final int resultSize) throws Exception { + + boolean voidMethod = isVoidMethod(ctx); + boolean booleanMethod = isBooleanMethod(ctx); + Object result = new ArrayList(resultSize); + boolean boolResult = false; + for (List l : listOfLists) { + parameters[i] = l; + ctx.setParameters(parameters); + Object resultObj = ctx.proceed(); + + if (voidMethod) { + continue; + } else if (isBooleanMethod(ctx) && resultObj != null) { + boolResult = boolResult && (Boolean) resultObj; + } else { + List tList = (List) resultObj; + if (tList != null) { + ((List) result).addAll(tList); + } + } + } + return booleanMethod ? boolResult : result; + } + + private boolean isNoBigList(final List source) { + + return source != null && source.size() <= MAX_LIST_SIZE; + } + + private boolean isVoidMethod(final InvocationContext ctx) { + + return ctx.getMethod().getReturnType() == void.class; + } + + private boolean isBooleanMethod(final InvocationContext ctx) { + + return ctx.getMethod().getReturnType() == boolean.class || ctx.getMethod().getReturnType() == Boolean.class; + } + +} diff --git a/example/src/main/java/biz/hahamo/dev/javaee/example/CollectionUtils.java b/example/src/main/java/biz/hahamo/dev/javaee/example/CollectionUtils.java new file mode 100755 index 0000000..27f0e6e --- /dev/null +++ b/example/src/main/java/biz/hahamo/dev/javaee/example/CollectionUtils.java @@ -0,0 +1,43 @@ +package biz.hahamo.dev.javaee.example; + +import java.util.ArrayList; +import java.util.List; + +/** + * Utility class to split long lists into a list of smaller lists + * + * @author GHajba + * + */ +public class CollectionUtils { + + protected CollectionUtils() { + + } + + public static List> splitLongList(final List source, final int size) { + + if (source == null) { + return null; // you could use org.apache.commons.lang.Validate.notNull(source); + } + + int necessaryLists = (int) Math.ceil(source.size() / (float) size); + List> sList = new ArrayList>(necessaryLists); + + if (size == 0 || source.size() <= size) { + sList.add(source); + return sList; + } + + for (int i = 0; i < necessaryLists; i++) { + if (i != (necessaryLists - 1)) { + sList.add(source.subList(i * size, (i + 1) * size)); + } else { + sList.add(source.subList(i * size, source.size())); + } + } + + return sList; + } + +} diff --git a/example/src/main/java/biz/hahamo/dev/javaee/example/Ora01795Repo.java b/example/src/main/java/biz/hahamo/dev/javaee/example/Ora01795Repo.java new file mode 100755 index 0000000..3d425f8 --- /dev/null +++ b/example/src/main/java/biz/hahamo/dev/javaee/example/Ora01795Repo.java @@ -0,0 +1,50 @@ +package biz.hahamo.dev.javaee.example; + +import java.util.ArrayList; +import java.util.List; + +import javax.interceptor.Interceptors; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +/** + * Simple repository to demonstrate the ORA-01795 exception and its avoiding with interceptors. + * + * @author GHajba + * + */ +public class Ora01795Repo { + + /** + * + */ + private static final int _1001 = 1001; + @PersistenceContext + private EntityManager entityManager; + + public void createOra01795() { + + Query query = entityManager.createQuery("FROM Fehler WHERE id in (:ids)", String.class); + query.setParameter("ids", generateBigList()); + query.getResultList(); + } + + @Interceptors(BigListInterceptor.class) + public void handleOra01795(final List parameters) { + + Query query = entityManager.createQuery("FROM Fehler WHERE id in (:ids)", String.class); + query.setParameter("ids", parameters); + query.getResultList(); + } + + private List generateBigList() { + + List idList = new ArrayList<>(_1001); + for (long i = 0; i < _1001; i++) { + idList.add(i); + } + return idList; + } + +} diff --git a/example/src/main/java/biz/hahamo/dev/javaee/example/TestEntity.java b/example/src/main/java/biz/hahamo/dev/javaee/example/TestEntity.java new file mode 100755 index 0000000..68c2f0f --- /dev/null +++ b/example/src/main/java/biz/hahamo/dev/javaee/example/TestEntity.java @@ -0,0 +1,22 @@ +package biz.hahamo.dev.javaee.example; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.Id; + +/** + * Simple example entity to demonstrate an ORA-01795 exception. + * + * @author GHajba + * + */ +@Entity +public class TestEntity implements Serializable { + + private static final long serialVersionUID = 4466268135555891310L; + + @Id + private Long id; + +} diff --git a/example/src/main/webapp/WEB-INF/example-ds.xml b/example/src/main/webapp/WEB-INF/example-ds.xml index b2832cf..9a49a8e 100644 --- a/example/src/main/webapp/WEB-INF/example-ds.xml +++ b/example/src/main/webapp/WEB-INF/example-ds.xml @@ -18,20 +18,16 @@ - - - - jdbc:h2:file:example - h2 - - sa - sa - - + + + + jdbc:oracle:thin:@localhost:1521:test + oracle + + test01 + test01 + + From 1396c4c47a8e13f96ab6f4c956b80d280a216e86 Mon Sep 17 00:00:00 2001 From: GHajba Date: Wed, 15 Oct 2014 15:45:32 +0200 Subject: [PATCH 2/2] changed names of entities --- .../main/java/biz/hahamo/dev/javaee/example/Ora01795Repo.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/example/src/main/java/biz/hahamo/dev/javaee/example/Ora01795Repo.java b/example/src/main/java/biz/hahamo/dev/javaee/example/Ora01795Repo.java index 3d425f8..e76def1 100755 --- a/example/src/main/java/biz/hahamo/dev/javaee/example/Ora01795Repo.java +++ b/example/src/main/java/biz/hahamo/dev/javaee/example/Ora01795Repo.java @@ -25,7 +25,7 @@ public class Ora01795Repo { public void createOra01795() { - Query query = entityManager.createQuery("FROM Fehler WHERE id in (:ids)", String.class); + Query query = entityManager.createQuery("FROM TestEntity WHERE id in (:ids)", String.class); query.setParameter("ids", generateBigList()); query.getResultList(); } @@ -33,7 +33,7 @@ public void createOra01795() { @Interceptors(BigListInterceptor.class) public void handleOra01795(final List parameters) { - Query query = entityManager.createQuery("FROM Fehler WHERE id in (:ids)", String.class); + Query query = entityManager.createQuery("FROM TestEntity WHERE id in (:ids)", String.class); query.setParameter("ids", parameters); query.getResultList(); }