4 Replies Latest reply on Oct 27, 2011 6:46 AM by 886397

    Can we always use NOT_SUPPORTED and SUPPORTED for all query method?

    886397
      Hi all,

      Since select from database doesn't need a transaction (Please correct me if it's wrong), can I set the transaction level to 'NOT SUPPORTED' or 'SUPPORTED' for all of these methods that only query something from database?

      Maybe these methods will be invoked in others that in a transaction, so 'NEVER' may not be used.

      My question is: is there any reason to get a transaction for methods without any insert/update in database?

      Say there are three beans as below:

      @Stateless
      @TransactionManagement(TransactionManagementType.CONTAINER)
      public class InsertBean implements Insert {       
                @Override
      @TransactionAttribute(TransactionAttributeType.REQUIRED)
      public void createUsers(List<User> users) {
      // INSERT INTO user WHERE .....
      }
      }

      @Stateless
      @TransactionManagement(TransactionManagementType.CONTAINER)     
      public class QueryBean implements Query {     
      @Override
      @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
      public int getUserNumber() {
      // SELECT COUNT(*) FROM user
      }
      }

      @Stateless
      @TransactionManagement(TransactionManagementType.CONTAINER)     
      public class UpdateBean implements Update {     
      @Override
      @TransactionAttribute(TransactionAttributeType.REQUIRED)
      public void updateUsers(List<User> users) {
      // UPDATE user SET logindate = xxx WHERE .....
      }
      }

      Insert and update requires a transaction and query not. If there is a bean method called like :

      InsertBean.createUsers(users);
      int n = QueryBean.getUserNumber();
      UpdateBean.updateUsers(users);

      For this case the transaction level for query method getUserNumber() must not be 'NEVER', only 'SUPPORTED' or 'NOT_SUPPROTED' is reasonable. But 'NOT_SUPPORTED' is more efficient. So can I use 'NOT_SUPPORTED' level for all query methods?

      If sometimes query method must be in a transaction would you please take a share?

      Thanks in advance.