From 692be9f5aeb0f3a89c693339b1810a9f1844a2fe Mon Sep 17 00:00:00 2001 From: erik Date: Sun, 2 May 2010 20:57:06 +0000 Subject: [PATCH] Now working with both hibernate and eclipselink --- README.txt | 4 + pom.xml | 2 +- .../AbstractAuthorizationService.java | 14 ++ .../DefaultAuthorizationService.java | 16 +- .../authorization/UrlAuthorizationRule.java | 7 +- .../jpa/JpaAuthorizationService.java | 16 +- .../main/java/org/wamblee/usermgt/User.java | 7 +- .../usermgt/UserAdministrationImpl.java | 37 ++++- .../org/wamblee/usermgt/jpa/JpaGroupSet.java | 1 + .../org/wamblee/usermgt/jpa/JpaUserSet.java | 8 +- .../AuthorizationServiceTest.java | 1 + security/jpatest/pom.xml | 137 ++++++++++-------- .../jpa/JpaAuthorizationServiceTest.java | 13 +- .../jpa/JpaUserAdministrationTest.java | 3 +- test/eclipselink/pom.xml | 113 ++++++++------- .../eclipselink/EclipselinkJpaCustomizer.java | 3 + .../support/jndi/StubInitialContext.java | 15 +- .../support/persistence/JpaBuilder.java | 20 ++- .../toplink/ToplinkJpaCustomizer.java | 3 + 19 files changed, 256 insertions(+), 164 deletions(-) diff --git a/README.txt b/README.txt index a41ba437..26f0a2ff 100644 --- a/README.txt +++ b/README.txt @@ -28,6 +28,10 @@ Building You need maven2 and java 6 +By default eclipselink is used as persistence provider. +Use mvn -Phibernate,!eclipselink (escape the ! if needed) to build with hibernate as +persistence provider. + Releasing ========= diff --git a/pom.xml b/pom.xml index 34cde990..a5a9652c 100644 --- a/pom.xml +++ b/pom.xml @@ -325,7 +325,7 @@ org.eclipse.persistence eclipselink - 2.0.0 + 2.0.2 compile diff --git a/security/impl/src/main/java/org/wamblee/security/authorization/AbstractAuthorizationService.java b/security/impl/src/main/java/org/wamblee/security/authorization/AbstractAuthorizationService.java index d8c1e527..8d79a95e 100644 --- a/security/impl/src/main/java/org/wamblee/security/authorization/AbstractAuthorizationService.java +++ b/security/impl/src/main/java/org/wamblee/security/authorization/AbstractAuthorizationService.java @@ -55,12 +55,26 @@ public abstract class AbstractAuthorizationService implements AuthorizationServi @Version private int version; + /** + * Name for this instance of the authorization service. + */ + private String name; + public AbstractAuthorizationService() { // Empty. } + public AbstractAuthorizationService(String aName) { + name = aName; + } + public AbstractAuthorizationService(AbstractAuthorizationService aSvc) { primaryKey = aSvc.primaryKey; version = aSvc.version; + name = aSvc.name; + } + + public String getName() { + return name; } } diff --git a/security/impl/src/main/java/org/wamblee/security/authorization/DefaultAuthorizationService.java b/security/impl/src/main/java/org/wamblee/security/authorization/DefaultAuthorizationService.java index 8ee41178..1e5abb99 100644 --- a/security/impl/src/main/java/org/wamblee/security/authorization/DefaultAuthorizationService.java +++ b/security/impl/src/main/java/org/wamblee/security/authorization/DefaultAuthorizationService.java @@ -57,11 +57,6 @@ public class DefaultAuthorizationService extends AbstractAuthorizationService { @Transient private UserAccessor userAccessor; - /** - * Name for this instance of the authorization service. - */ - private String name; - /** * Constructs the service. * @@ -71,9 +66,9 @@ public class DefaultAuthorizationService extends AbstractAuthorizationService { * Name of this instance of the service. */ public DefaultAuthorizationService(UserAccessor aAccessor, String aName) { + super(aName); rules = new ArrayList(); userAccessor = aAccessor; - name = aName; } /** @@ -82,7 +77,6 @@ public class DefaultAuthorizationService extends AbstractAuthorizationService { public DefaultAuthorizationService() { rules = new ArrayList(); userAccessor = null; - name = null; } /** @@ -133,14 +127,6 @@ public class DefaultAuthorizationService extends AbstractAuthorizationService { return aResource; } - protected String getName() { - return name; - } - - public void setName(String aName) { - name = aName; - } - /* * (non-Javadoc) * diff --git a/security/impl/src/main/java/org/wamblee/security/authorization/UrlAuthorizationRule.java b/security/impl/src/main/java/org/wamblee/security/authorization/UrlAuthorizationRule.java index 17bdc348..e782a18e 100644 --- a/security/impl/src/main/java/org/wamblee/security/authorization/UrlAuthorizationRule.java +++ b/security/impl/src/main/java/org/wamblee/security/authorization/UrlAuthorizationRule.java @@ -26,6 +26,7 @@ import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; +import javax.persistence.OneToOne; import javax.persistence.Transient; import org.apache.log4j.Logger; @@ -264,7 +265,7 @@ public abstract class UrlAuthorizationRule extends AbstractAuthorizationRule { * * @return Returns the operationCondition. */ - @ManyToOne(cascade = CascadeType.ALL, targetEntity = AbstractOperationCondition.class) + @OneToOne(cascade = CascadeType.ALL, targetEntity = AbstractOperationCondition.class, orphanRemoval = true) @JoinColumn(name = "OPER_COND_PK") public OperationCondition getOperationCondition() { return operationCondition; @@ -283,7 +284,7 @@ public abstract class UrlAuthorizationRule extends AbstractAuthorizationRule { * * @return Returns the pathCondition. */ - @ManyToOne(cascade = CascadeType.ALL, targetEntity = AbstractPathCondition.class) + @OneToOne(cascade = CascadeType.ALL, targetEntity = AbstractPathCondition.class, orphanRemoval = true) @JoinColumn(name = "PATH_COND_PK") public PathCondition getPathCondition() { return pathCondition; @@ -302,7 +303,7 @@ public abstract class UrlAuthorizationRule extends AbstractAuthorizationRule { * * @return Returns the userCondition. */ - @ManyToOne(cascade = CascadeType.ALL, targetEntity = AbstractUserCondition.class) + @OneToOne(cascade = CascadeType.ALL, targetEntity = AbstractUserCondition.class, orphanRemoval = true) @JoinColumn(name = "USER_COND_PK") public UserCondition getUserCondition() { return userCondition; diff --git a/security/impl/src/main/java/org/wamblee/security/authorization/jpa/JpaAuthorizationService.java b/security/impl/src/main/java/org/wamblee/security/authorization/jpa/JpaAuthorizationService.java index e1b71d7d..57d29636 100644 --- a/security/impl/src/main/java/org/wamblee/security/authorization/jpa/JpaAuthorizationService.java +++ b/security/impl/src/main/java/org/wamblee/security/authorization/jpa/JpaAuthorizationService.java @@ -100,16 +100,17 @@ public class JpaAuthorizationService implements AuthorizationService { */ private void initialize() { if (service == null) { - refreshByReload(); + service = refreshByReload(); } } - private void refreshByReload() { + private AuthorizationService refreshByReload() { + AuthorizationService service; try { service = entityManager.createNamedQuery( AbstractAuthorizationService.QUERY_FIND_BY_NAME, AbstractAuthorizationService.class).setParameter( - AbstractAuthorizationService.NAME_PARAM, name).getSingleResult(); + DefaultAuthorizationService.NAME_PARAM, name).getSingleResult(); service.setUserAccessor(userAccessor); } catch (NonUniqueResultException e) { throw new IllegalArgumentException( @@ -118,6 +119,7 @@ public class JpaAuthorizationService implements AuthorizationService { service = new DefaultAuthorizationService(userAccessor, name); entityManager.persist(service); } + return service; } /* @@ -170,7 +172,7 @@ public class JpaAuthorizationService implements AuthorizationService { initialize(); refresh(); service.appendRule(aRule); - save(); + save(); // service might still be detached as service is cached. } /* @@ -183,7 +185,7 @@ public class JpaAuthorizationService implements AuthorizationService { initialize(); refresh(); service.removeRule(aIndex); - save(); + save(); // service might still be detached as service is cached. } /* @@ -197,7 +199,7 @@ public class JpaAuthorizationService implements AuthorizationService { initialize(); refresh(); service.insertRuleAfter(aIndex, aRule); - save(); + save(); // service might still be detached as service is cached. } /** @@ -207,7 +209,7 @@ public class JpaAuthorizationService implements AuthorizationService { long time = System.currentTimeMillis(); if ((time - lastRefreshTime) > refreshInterval) { - refreshByReload(); + service = refreshByReload(); lastRefreshTime = time; } } diff --git a/security/impl/src/main/java/org/wamblee/usermgt/User.java b/security/impl/src/main/java/org/wamblee/usermgt/User.java index 7462507a..0e1c5453 100644 --- a/security/impl/src/main/java/org/wamblee/usermgt/User.java +++ b/security/impl/src/main/java/org/wamblee/usermgt/User.java @@ -47,7 +47,7 @@ import org.wamblee.usermgt.UserMgtException.Reason; User.NAME_PARAM), @NamedQuery(name = User.QUERY_FIND_BY_GROUP_NAME, query = "select user from User user join user.groups grp where grp.name = :name"), @NamedQuery(name = User.QUERY_COUNT_USERS, query = "select count(u) from User u"), - @NamedQuery(name = User.QUERY_ALL_USERS, query = "select u from User u")}) + @NamedQuery(name = User.QUERY_ALL_USERS, query = "select u from User u") }) public class User implements Serializable, Comparable { public static final String QUERY_FIND_BY_NAME = "User.findByName"; @@ -119,7 +119,7 @@ public class User implements Serializable, Comparable { * */ public User(User aUser) { - primaryKey = aUser.primaryKey; + primaryKey = aUser.primaryKey; version = aUser.version; name = aUser.name; password = aUser.password; @@ -356,6 +356,9 @@ public class User implements Serializable, Comparable { */ @Override public boolean equals(Object aUser) { + if (aUser == null) { + return false; + } if (!(aUser instanceof User)) { return false; } diff --git a/security/impl/src/main/java/org/wamblee/usermgt/UserAdministrationImpl.java b/security/impl/src/main/java/org/wamblee/usermgt/UserAdministrationImpl.java index 6866d454..3dc00e06 100644 --- a/security/impl/src/main/java/org/wamblee/usermgt/UserAdministrationImpl.java +++ b/security/impl/src/main/java/org/wamblee/usermgt/UserAdministrationImpl.java @@ -12,7 +12,7 @@ * 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.wamblee.usermgt; import static org.wamblee.usermgt.UserMgtException.Reason.DUPLICATE_GROUP; @@ -22,6 +22,7 @@ import static org.wamblee.usermgt.UserMgtException.Reason.TRIVIAL_RENAME; import static org.wamblee.usermgt.UserMgtException.Reason.UNKNOWN_GROUP; import static org.wamblee.usermgt.UserMgtException.Reason.UNKNOWN_USER; +import java.util.HashSet; import java.util.Set; /** @@ -125,7 +126,11 @@ public class UserAdministrationImpl implements UserAdministration { * @see org.wamblee.usermgt.UserAdministration#getUser(java.lang.String) */ public User getUser(String aName) { - return users.find(aName); + User user = users.find(aName); + if (user == null) { + return user; + } + return new User(user); } /* @@ -134,7 +139,11 @@ public class UserAdministrationImpl implements UserAdministration { * @see org.wamblee.usermgt.UserAdministration#getGroup(java.lang.String) */ public Group getGroup(String aName) { - return groups.find(aName); + Group group = groups.find(aName); + if ( group == null ) { + return group; + } + return new Group(group); } /* @@ -143,7 +152,11 @@ public class UserAdministrationImpl implements UserAdministration { * @see org.wamblee.usermgt.UserAdministration#getUsers() */ public Set getUsers() { - return users.list(); + Set res = new HashSet(); + for (User user: users.list()) { + res.add(new User(user)); + } + return res; } /* @@ -154,7 +167,11 @@ public class UserAdministrationImpl implements UserAdministration { * ) */ public Set getUsers(Group aGroup) { - return users.list(aGroup); + Set res = new HashSet(); + for (User user: users.list(aGroup)) { + res.add(new User(user)); + } + return res; } /* @@ -163,7 +180,11 @@ public class UserAdministrationImpl implements UserAdministration { * @see org.wamblee.usermgt.UserAdministration#getGroups() */ public Set getGroups() { - return groups.list(); + Set res = new HashSet(); + for (Group group: groups.list()) { + res.add(new Group(group)); + } + return res; } /* @@ -215,7 +236,7 @@ public class UserAdministrationImpl implements UserAdministration { } userValidator.validate(aUserName); - + aUser.setName(aUserName); users.userModified(aUser); } @@ -240,7 +261,7 @@ public class UserAdministrationImpl implements UserAdministration { } groupValidator.validate(aGroupName); - + aGroup.setName(aGroupName); groups.groupModified(aGroup); } diff --git a/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java b/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java index 2150b43a..a9d579e2 100644 --- a/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java +++ b/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaGroupSet.java @@ -46,6 +46,7 @@ public class JpaGroupSet implements GroupSet { return false; } em.persist(aGroup); + em.flush(); // to make sure the version is updated. return true; } diff --git a/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaUserSet.java b/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaUserSet.java index 045eda00..12a5dabf 100644 --- a/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaUserSet.java +++ b/security/impl/src/main/java/org/wamblee/usermgt/jpa/JpaUserSet.java @@ -124,6 +124,7 @@ public class JpaUserSet extends AbstractUserSet { } entityManager.persist(aUser); + entityManager.flush(); // to make sure the version is updated. setPasswordInfo(aUser); cache.put(aUser.getName(), aUser); @@ -159,7 +160,7 @@ public class JpaUserSet extends AbstractUserSet { for (User user : list) { setPasswordInfo(user); - users.add(new User(user)); + users.add(user); } return users; @@ -177,10 +178,9 @@ public class JpaUserSet extends AbstractUserSet { query.setParameter(User.NAME_PARAM, aGroup.getName()); List list = query.getResultList(); - - for (User user : list) { + users.addAll(list); + for (User user : users) { setPasswordInfo(user); - users.add(new User(user)); } return users; diff --git a/security/impl/src/test/java/org/wamblee/security/authorization/AuthorizationServiceTest.java b/security/impl/src/test/java/org/wamblee/security/authorization/AuthorizationServiceTest.java index 8be34f9d..8b1f1e15 100644 --- a/security/impl/src/test/java/org/wamblee/security/authorization/AuthorizationServiceTest.java +++ b/security/impl/src/test/java/org/wamblee/security/authorization/AuthorizationServiceTest.java @@ -56,6 +56,7 @@ public class AuthorizationServiceTest extends TestCase { service.appendRule(rule1); service.appendRule(rule2); service.appendRule(rule3); + checkRuleCount(3); } protected void resetTestRules() { diff --git a/security/jpatest/pom.xml b/security/jpatest/pom.xml index 83b96e8f..6fc29ff0 100644 --- a/security/jpatest/pom.xml +++ b/security/jpatest/pom.xml @@ -1,64 +1,87 @@ - + - + + org.wamblee + wamblee-utils + 0.2.3-SNAPSHOT + + + 4.0.0 org.wamblee - wamblee-utils - 0.2.3-SNAPSHOT - + wamblee-security-jpatest + jar + /security/jpatest + http://wamblee.org + - 4.0.0 - org.wamblee - wamblee-security-jpatest - jar - /security/jpatest - http://wamblee.org - + + org.wamblee + wamblee-security-impl + 0.2.3-SNAPSHOT + + + org.wamblee + wamblee-security-impl + 0.2.3-SNAPSHOT + test-jar + test + - - org.hibernate - hibernate-entitymanager - 3.5.0-Final - - - javax.transaction - jta - - - - - org.wamblee - wamblee-security-impl - 0.2.3-SNAPSHOT - - - org.wamblee - wamblee-security-impl - 0.2.3-SNAPSHOT - test-jar - test - - - - org.wamblee - wamblee-test-enterprise - 0.2.3-SNAPSHOT - test - - - org.wamblee - wamblee-test-hibernate - 0.2.3-SNAPSHOT - test - - - + + org.wamblee + wamblee-test-enterprise + 0.2.3-SNAPSHOT + test + - - - jboss - JBoss Repo - http://repository.jboss.org/maven2 - - + + + + eclipselink + + true + + + + org.wamblee + wamblee-test-eclipselink + 0.2.3-SNAPSHOT + test + + + + + jboss + JBoss Repo + http://repository.jboss.org/maven2 + + + + + hibernate + + + org.hibernate + hibernate-entitymanager + 3.5.0-Final + + + javax.transaction + jta + + + + + org.wamblee + wamblee-test-hibernate + 0.2.3-SNAPSHOT + test + + + + + diff --git a/security/jpatest/src/test/java/org/wamblee/security/authorization/jpa/JpaAuthorizationServiceTest.java b/security/jpatest/src/test/java/org/wamblee/security/authorization/jpa/JpaAuthorizationServiceTest.java index 1573f473..d67e09c4 100644 --- a/security/jpatest/src/test/java/org/wamblee/security/authorization/jpa/JpaAuthorizationServiceTest.java +++ b/security/jpatest/src/test/java/org/wamblee/security/authorization/jpa/JpaAuthorizationServiceTest.java @@ -15,15 +15,16 @@ */ package org.wamblee.security.authorization.jpa; +import static org.wamblee.security.authorization.AuthorizationResult.*; + import javax.persistence.EntityManager; import org.apache.log4j.Logger; -import org.wamblee.security.authorization.AbstractAuthorizationService; import org.wamblee.security.authorization.AllOperation; -import org.wamblee.security.authorization.AuthorizationResult; +import org.wamblee.security.authorization.AuthorizationRule; import org.wamblee.security.authorization.AuthorizationService; import org.wamblee.security.authorization.AuthorizationServiceTest; -import org.wamblee.security.authorization.TestAuthorizationRule; +import org.wamblee.security.authorization.DefaultAuthorizationService; import org.wamblee.support.persistence.JpaTester; import org.wamblee.support.persistence.TransactionProxyFactory; import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork; @@ -62,6 +63,12 @@ public class JpaAuthorizationServiceTest extends AuthorizationServiceTest { super.setUp(); } + @Override + protected void tearDown() throws Exception { + jpaTester.stop(); + super.tearDown(); + } + /* * (non-Javadoc) * diff --git a/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaUserAdministrationTest.java b/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaUserAdministrationTest.java index a0c0eb1a..e823d944 100644 --- a/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaUserAdministrationTest.java +++ b/security/jpatest/src/test/java/org/wamblee/usermgt/jpa/JpaUserAdministrationTest.java @@ -20,6 +20,7 @@ import java.sql.Connection; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.junit.Ignore; import org.wamblee.cache.EhCache; import org.wamblee.io.ClassPathResource; import org.wamblee.security.encryption.Md5HexMessageDigester; @@ -105,7 +106,7 @@ public class JpaUserAdministrationTest extends UserAdministrationImplTest { protected UserAdministration createAdmin() { return userAdmin; } - + public void testAllTestsInASeparateTransaction() throws Exception { Method[] methods = UserAdministrationImplTest.class.getMethods(); diff --git a/test/eclipselink/pom.xml b/test/eclipselink/pom.xml index 23c6f5f7..bfb2dda5 100644 --- a/test/eclipselink/pom.xml +++ b/test/eclipselink/pom.xml @@ -1,56 +1,63 @@ - - - - org.wamblee - wamblee-utils - 0.2.3-SNAPSHOT - - - 4.0.0 - org.wamblee - wamblee-test-eclipselink - jar - /test/eclipselink - http://wamblee.org - - - - org.wamblee - wamblee-test-enterprise - 0.2.3-SNAPSHOT - - - - org.wamblee - wamblee-test-enterprise - 0.2.3-SNAPSHOT - test-jar - - - - org.dbunit - dbunit - - - - javax.persistence - persistence-api - - - - org.eclipse.persistence - eclipselink - - - - - - - EclipseLink Repo - http://www.eclipse.org/downloads/download.php?r=1&nf=1&file=/rt/eclipselink/maven.repo - - - + + + + org.wamblee + wamblee-utils + 0.2.3-SNAPSHOT + + + 4.0.0 + org.wamblee + wamblee-test-eclipselink + jar + /test/eclipselink + http://wamblee.org + + + + org.wamblee + wamblee-test-enterprise + 0.2.3-SNAPSHOT + + + + org.wamblee + wamblee-test-enterprise + 0.2.3-SNAPSHOT + test-jar + + + + org.dbunit + dbunit + + + + org.eclipse.persistence + javax.persistence + 2.0.0 + + + + javax.persistence + persistence-api + + + + org.eclipse.persistence + eclipselink + + + + + + + EclipseLink Repo + http://www.eclipse.org/downloads/download.php?r=1&nf=1&file=/rt/eclipselink/maven.repo + + + diff --git a/test/eclipselink/src/main/java/org/wamblee/support/persistence/eclipselink/EclipselinkJpaCustomizer.java b/test/eclipselink/src/main/java/org/wamblee/support/persistence/eclipselink/EclipselinkJpaCustomizer.java index 36862965..97df5de9 100644 --- a/test/eclipselink/src/main/java/org/wamblee/support/persistence/eclipselink/EclipselinkJpaCustomizer.java +++ b/test/eclipselink/src/main/java/org/wamblee/support/persistence/eclipselink/EclipselinkJpaCustomizer.java @@ -44,6 +44,9 @@ public class EclipselinkJpaCustomizer implements JpaCustomizer { // DDL generation for toplink aJpaProperties.put("eclipselink.ddl-generation", "create-tables"); + + // Use JTA transaction type + aJpaProperties.put("javax.persistence.transactionType", "JTA"); } @Override diff --git a/test/enterprise/src/main/java/org/wamblee/support/jndi/StubInitialContext.java b/test/enterprise/src/main/java/org/wamblee/support/jndi/StubInitialContext.java index 4db8a112..5300a8b6 100644 --- a/test/enterprise/src/main/java/org/wamblee/support/jndi/StubInitialContext.java +++ b/test/enterprise/src/main/java/org/wamblee/support/jndi/StubInitialContext.java @@ -12,7 +12,7 @@ * 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.wamblee.support.jndi; import java.util.HashMap; @@ -20,6 +20,7 @@ import java.util.Map; import javax.naming.InitialContext; import javax.naming.Name; +import javax.naming.NameNotFoundException; import javax.naming.NamingException; public class StubInitialContext extends InitialContext { @@ -36,11 +37,19 @@ public class StubInitialContext extends InitialContext { @Override public Object lookup(String aName) throws NamingException { - return bindings.get(aName); + Object value = bindings.get(aName); + if (value == null) { + throw new NameNotFoundException(aName); + } + return value; } @Override public Object lookup(Name aName) throws NamingException { - return super.lookup(aName.toString()); + Object value = super.lookup(aName.toString()); + if (value == null) { + throw new NameNotFoundException(aName.toString()); + } + return value; } } diff --git a/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java b/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java index 4d1921f5..d3b7e179 100644 --- a/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java +++ b/test/enterprise/src/main/java/org/wamblee/support/persistence/JpaBuilder.java @@ -12,7 +12,7 @@ * 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.wamblee.support.persistence; import java.util.Map; @@ -26,6 +26,7 @@ import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; +import javax.persistence.PersistenceException; import javax.sql.DataSource; import org.wamblee.support.jndi.StubInitialContextFactory; @@ -93,12 +94,17 @@ public class JpaBuilder { throw new RuntimeException("JNDI problem", e); } factory = createFactory(); - execute(new JpaUnitOfWork() { - public Void execute(EntityManager aEm) { - // Empty, just to trigger database schema creation. - return null; - } - }); + try { + execute(new JpaUnitOfWork() { + public Void execute(EntityManager aEm) { + // Empty, just to trigger database schema creation. + return null; + } + }); + } catch (PersistenceException e) { + factory.close(); + throw e; + } } /** diff --git a/test/toplink-essentials/src/main/java/org/wamblee/support/persistence/toplink/ToplinkJpaCustomizer.java b/test/toplink-essentials/src/main/java/org/wamblee/support/persistence/toplink/ToplinkJpaCustomizer.java index 26fe6a54..f9be8e47 100644 --- a/test/toplink-essentials/src/main/java/org/wamblee/support/persistence/toplink/ToplinkJpaCustomizer.java +++ b/test/toplink-essentials/src/main/java/org/wamblee/support/persistence/toplink/ToplinkJpaCustomizer.java @@ -44,6 +44,9 @@ public class ToplinkJpaCustomizer implements JpaCustomizer { // DDL generation for toplink aJpaProperties.put("toplink.ddl-generation", "create-tables"); + + // Use JTA transaction type + aJpaProperties.put("javax.persistence.transactionType", "JTA"); } @Override -- 2.31.1