X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=test%2Fenterprise%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fsupport%2Fpersistence%2FJpaBuilder.java;h=e4cdcfbf7ce3110a88c622f1328d90be22804326;hb=993a2f8b4cfe42af68cfb9b9d0a801179090815c;hp=4e824093b14d61c816f9c4a1d8daa8b964b45a65;hpb=17775e14ecfb286e59f67117e5cee7e21e95ab1f;p=utils
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 4e824093..e4cdcfbf 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;
@@ -20,13 +20,11 @@ import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
-import javax.sql.DataSource;
+import javax.persistence.PersistenceException;
import org.wamblee.support.jndi.StubInitialContextFactory;
@@ -52,32 +50,36 @@ public class JpaBuilder {
/**
* Executes the unit of work. A transaction has been started.
*
- * @param em
+ * @param aEm
* Entity manager.
* @return Result of the execute method. If you don't want to return
* anything use Void
for the return type and return
* null from the implementation.
*/
- T execute(EntityManager em);
+ T execute(EntityManager aEm) throws Exception;
}
private PersistenceUnitDescription persistenceUnit;
- private DataSource dataSource;
+ private String url;
+ private String user;
+ private String password;
private EntityManagerFactory factory;
/**
* Constructs the builder.
*
- * @param aDataSource
- * Datasource of database.
+ * @param aUrl JDBC URL
+ * @param aUser User name
+ * @param aPassword Password.
* @param aPersistenceUnit
* Persistence unit.
*/
- public JpaBuilder(DataSource aDataSource,
+ public JpaBuilder(String aUrl, String aUser, String aPassword,
PersistenceUnitDescription aPersistenceUnit) {
persistenceUnit = aPersistenceUnit;
- dataSource = aDataSource;
- StubInitialContextFactory.register();
+ url = aUrl;
+ user = aUser;
+ password = aPassword;
}
/**
@@ -86,19 +88,18 @@ public class JpaBuilder {
* manager factory, and forces creation of the database schema.
*/
public void start() throws Exception {
+ factory = createFactory();
try {
- InitialContext ctx = new InitialContext();
- ctx.bind(persistenceUnit.getJndiName(), dataSource);
- } catch (NamingException e) {
- throw new RuntimeException("JNDI problem", e);
+ execute(new JpaUnitOfWork() {
+ public Void execute(EntityManager aEm) {
+ // Empty, just to trigger database schema creation.
+ return null;
+ }
+ });
+ } catch (PersistenceException e) {
+ factory.close();
+ throw e;
}
- factory = createFactory();
- execute(new JpaUnitOfWork() {
- public Void execute(EntityManager em) {
- // Empty, just to trigger database schema creation.
- return null;
- }
- });
}
/**
@@ -116,17 +117,23 @@ public class JpaBuilder {
*/
public EntityManagerFactory createFactory() {
Map jpaProps = new TreeMap();
+
+ jpaProps.put("javax.persistence.jtaDataSource", null);
+ jpaProps.put("javax.persistence.transactionType", "RESOURCE_LOCAL");
+ jpaProps.put("javax.persistence.jdbc.url", url);
+ jpaProps.put("javax.persistence.jdbc.user", user);
+ jpaProps.put("javax.persistence.jdbc.password", password);
JpaCustomizerBuilder.getCustomizer().customize(persistenceUnit,
jpaProps);
-
+
// jpaProps.put("javax.persistence.provider",
// HibernatePersistence.class.getName());
- EntityManagerFactory factory = Persistence.createEntityManagerFactory(
+ EntityManagerFactory emf = Persistence.createEntityManagerFactory(
persistenceUnit.getUnitName(), jpaProps);
- LOGGER.info("Using " + factory.getClass());
- return factory;
+ LOGGER.info("Using " + emf.getClass());
+ return emf;
}
/**