Commits

Anonymous committed c93e492

More enhancements to ejb3 support

git-svn-id: http://svn.opensymphony.com/svn/propertyset/trunk@180bf3cbcdd-1c1a-0410-9a68-d6f521e3fa7b

Comments (0)

Files changed (4)

providers/ejb3/src/META-INF/persistence.xml

-<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
   <persistence-unit name="propertyset">
     <description>Persistence Unit for OSPropertySet</description>
     <class>com.opensymphony.module.propertyset.ejb3.PropertyEntry</class>

providers/ejb3/src/com/opensymphony/module/propertyset/ejb3/EJBPropertySet.java

 import javax.persistence.EntityManager;
 import javax.persistence.EntityTransaction;
 import javax.persistence.Query;
+import javax.persistence.spi.PersistenceUnitTransactionType;
 
 
 /**
  * EJB3 propertyset implementation.
- * The only additional init arg required over entityId and entityName is 'manager', which is the
- * entity manager to use.
+ * This implementation requires a couple of extra init args:
+ * <li><code>manager</code>: Entity manager to use.
+ * <li><code>transaction</code>: Can be either JTA or RESOURCE_LOCAL.
  * @author Hani Suleiman
  *         Date: Nov 8, 2005
  *         Time: 4:51:53 PM
 
     private EntityManager entityManager;
     private Long entityId;
+    private PersistenceUnitTransactionType transactionType;
     private String entityName;
 
+    //~ Constructors ///////////////////////////////////////////////////////////
+
+    public EJBPropertySet() {
+    }
+
+    public EJBPropertySet(EntityManager entityManager, PersistenceUnitTransactionType transactionType) {
+        this.entityManager = entityManager;
+        this.transactionType = transactionType;
+    }
+
+    public EJBPropertySet(EntityManager entityManager, PersistenceUnitTransactionType transactionType, Long entityId, String entityName) {
+        this.entityManager = entityManager;
+        this.entityId = entityId;
+        this.entityName = entityName;
+        this.transactionType = transactionType;
+    }
+
     //~ Methods ////////////////////////////////////////////////////////////////
 
+    public void setEntityId(Long entityId) {
+        this.entityId = entityId;
+    }
+
+    public Long getEntityId() {
+        return entityId;
+    }
+
     public void setEntityManager(EntityManager entityManager) {
         this.entityManager = entityManager;
     }
         return entityManager;
     }
 
+    public void setEntityName(String entityName) {
+        this.entityName = entityName;
+    }
+
+    public String getEntityName() {
+        return entityName;
+    }
+
     public Collection getKeys(String prefix, int type) throws PropertyException {
         return getKeys(entityName, entityId, prefix, type);
     }
         return q.getResultList();
     }
 
+    public void setTransactionType(PersistenceUnitTransactionType transactionType) {
+        this.transactionType = transactionType;
+    }
+
+    public PersistenceUnitTransactionType getTransactionType() {
+        return transactionType;
+    }
+
     public int getType(String key) throws PropertyException {
         return getType(entityName, entityId, key);
     }
         this.entityManager = (EntityManager) obj;
         this.entityId = ((Number) args.get("entityId")).longValue();
         this.entityName = (String) args.get("entityName");
+
+        Object tx = args.get("transaction");
+        this.transactionType = (tx == null) ? PersistenceUnitTransactionType.RESOURCE_LOCAL : PersistenceUnitTransactionType.valueOf(tx.toString());
     }
 
     public void remove(String key) throws PropertyException {
 
         boolean mustCommit = false;
 
-        try {
+        switch (transactionType) {
+        case JTA:
             entityManager.joinTransaction();
-        } catch (Exception ex) {
+
+            break;
+
+        case RESOURCE_LOCAL:
+
             EntityTransaction tx = entityManager.getTransaction();
 
             if (!tx.isActive()) {
                 tx.begin();
                 mustCommit = true;
             }
+
+            break;
         }
 
         item = entityManager.find(PropertyEntry.class, pk);

providers/ejb3/src/com/opensymphony/module/propertyset/ejb3/PropertyEntry.java

 @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
 @org.hibernate.annotations.Table(appliesTo="PropertyEntry", indexes = 
 {
-  @Index(name="os_PropertyEntry_keyidx", columnNames = {"entityName","entityId", "keyName" }),
   @Index(name="os_PropertyEntry_allidx", columnNames = {"entityName","entityId"})
 })
 public class PropertyEntry

src/etc/persistence.xml

-<entity-manager>
-  <name>propertyset</name>
-  <jta-data-source>jdbc/DefaultDS</jta-data-source>
-</entity-manager>
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
+  <persistence-unit name="propertyset">
+    <description>Persistence Unit for OSPropertySet</description>
+    <class>com.opensymphony.module.propertyset.ejb3.PropertyEntry</class>
+    <exclude-unlisted-classes>true</exclude-unlisted-classes>
+  </persistence-unit>
+</persistence>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.