Skip to content

Commit

Permalink
[Blazebit#414] Handle non-Java EE JTA environments and skip updater r…
Browse files Browse the repository at this point in the history
…elated entity view updater initialization for read-only views
  • Loading branch information
beikov committed Dec 12, 2017
1 parent acf5194 commit 14cc22c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.blazebit.persistence.view.impl.tx;

import javax.naming.InitialContext;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.naming.NoInitialContextException;
import javax.persistence.EntityManager;
Expand All @@ -29,22 +30,37 @@
*/
public class TransactionHelper {

private static final String[] NAMES = {
"java:comp/TransactionSynchronizationRegistry", // Java EE Standard name
"java:/TransactionSynchronizationRegistry", // Local providers
"java:comp/env/TransactionSynchronizationRegistry", // Tomcat
};

private TransactionHelper() {
}

public static TransactionSynchronizationStrategy getSynchronizationStrategy(EntityManager em) {
TransactionSynchronizationRegistry synchronizationRegistry;
InitialContext context = null;

try {
synchronizationRegistry = (TransactionSynchronizationRegistry) new InitialContext().lookup("java:comp/TransactionSynchronizationRegistry");
if (synchronizationRegistry != null) {
return new JtaTransactionSynchronizationStrategy(synchronizationRegistry);
}
} catch (NoInitialContextException e) {
// Maybe in Java SE environment
synchronizationRegistry = null;
context = new InitialContext();
} catch (NamingException e) {
throw new IllegalArgumentException("Could not access transaction synchronization registry!", e);
// Maybe in Java SE environment
}

if (context != null) {
for (String name : NAMES) {
try {
TransactionSynchronizationRegistry synchronizationRegistry = (TransactionSynchronizationRegistry) context.lookup(name);
if (synchronizationRegistry != null) {
return new JtaTransactionSynchronizationStrategy(synchronizationRegistry);
}
} catch (NoInitialContextException | NameNotFoundException e) {
// Maybe in Java SE environment
} catch (NamingException e) {
throw new IllegalArgumentException("Could not access transaction synchronization registry!", e);
}
}
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ public EntityViewUpdaterImpl(EntityViewManagerImpl evm, ManagedViewTypeImplement
idViewBuilder = null;
this.idFlusher = null;
}
this.fullEntityLoader = new FullEntityLoader(evm, viewType);
boolean mutable = viewType.isCreatable() || viewType.isUpdatable();
if (mutable) {
this.fullEntityLoader = new FullEntityLoader(evm, viewType);
} else {
this.fullEntityLoader = null;
}
Set<MethodAttribute<?, ?>> attributes = (Set<MethodAttribute<?, ?>>) (Set<?>) viewType.getAttributes();
AbstractMethodAttribute<?, ?> idAttribute;
AbstractMethodAttribute<?, ?> versionAttribute;
Expand Down Expand Up @@ -188,7 +193,7 @@ public EntityViewUpdaterImpl(EntityViewManagerImpl evm, ManagedViewTypeImplement
StringBuilder sb = null;
int clauseEndIndex = -1;

if (flushStrategy != FlushStrategy.ENTITY && jpaIdAttribute != null) {
if (mutable && flushStrategy != FlushStrategy.ENTITY && jpaIdAttribute != null) {
this.updatePrefixString = "UPDATE " + entityType.getName() + " e SET ";
if (versionAttribute != null) {
this.updatePostfixString = " WHERE e." + jpaIdAttribute.getName() + " = :" + ID_PARAM_NAME +
Expand All @@ -214,7 +219,7 @@ public EntityViewUpdaterImpl(EntityViewManagerImpl evm, ManagedViewTypeImplement
}

// Only construct attribute flushers for creatable or updatable entity views
if (viewType.isCreatable() || viewType.isUpdatable()) {
if (mutable) {
for (MethodAttribute<?, ?> attribute : attributes) {
if (attribute == idAttribute || attribute == versionAttribute) {
continue;
Expand Down Expand Up @@ -267,7 +272,7 @@ public EntityViewUpdaterImpl(EntityViewManagerImpl evm, ManagedViewTypeImplement
viewType.getFlushMode(),
flushStrategy
);
if (flushStrategy != FlushStrategy.ENTITY && jpaIdAttribute != null && clauseEndIndex != sb.length()) {
if (mutable && flushStrategy != FlushStrategy.ENTITY && jpaIdAttribute != null && clauseEndIndex != sb.length()) {
if (clauseEndIndex + 2 == sb.length()) {
// Remove the last comma
sb.setLength(clauseEndIndex);
Expand Down

0 comments on commit 14cc22c

Please sign in to comment.