@Transactional
 public AccessKey authenticate(@NotNull String key) {
   Optional<AccessKey> accessKeyOpt =
       genericDAO
           .createNamedQuery(AccessKey.class, "AccessKey.getByKey", Optional.of(CacheConfig.get()))
           .setParameter("someKey", key)
           .getResultList()
           .stream()
           .findFirst();
   if (!accessKeyOpt.isPresent()) {
     return null;
   }
   AccessKey accessKey = accessKeyOpt.get();
   final Long expirationPeriod =
       configurationService.getLong(Constants.SESSION_TIMEOUT, Constants.DEFAULT_SESSION_TIMEOUT);
   if (accessKey.getExpirationDate() != null) {
     final Long expiresIn =
         accessKey.getExpirationDate().getTime() - timestampService.getTimestamp().getTime();
     if (AccessKeyType.SESSION == accessKey.getType()
         && expiresIn > 0
         && expiresIn < expirationPeriod / 2) {
       em.refresh(accessKey, LockModeType.PESSIMISTIC_WRITE);
       accessKey.setExpirationDate(
           new Date(timestampService.getTimestamp().getTime() + expirationPeriod));
       return genericDAO.merge(accessKey);
     }
   }
   return accessKey;
 }
 @AroundInvoke
 public Object checkPermissions(InvocationContext context) throws Exception {
   try {
     logger.trace(Thread.currentThread().getName());
     HivePrincipal principal = ThreadLocalVariablesKeeper.getPrincipal();
     AccessKey key = principal.getKey();
     if (key == null) {
       return context.proceed();
     }
     if (key.getUser() == null || !key.getUser().getStatus().equals(UserStatus.ACTIVE)) {
       throw new HiveException(UNAUTHORIZED.getReasonPhrase(), UNAUTHORIZED.getStatusCode());
     }
     Timestamp expirationDate = key.getExpirationDate();
     if (expirationDate != null
         && expirationDate.before(new Timestamp(System.currentTimeMillis()))) {
       throw new HiveException(UNAUTHORIZED.getReasonPhrase(), UNAUTHORIZED.getStatusCode());
     }
     Method method = context.getMethod();
     AllowedKeyAction allowedActionAnnotation = method.getAnnotation(AllowedKeyAction.class);
     List<AllowedKeyAction.Action> actions = Arrays.asList(allowedActionAnnotation.action());
     boolean isAllowed = CheckPermissionsHelper.checkAllPermissions(key, actions);
     if (!isAllowed) {
       throw new HiveException(UNAUTHORIZED.getReasonPhrase(), UNAUTHORIZED.getStatusCode());
     }
     return context.proceed();
   } finally {
     ThreadLocalVariablesKeeper.clean();
   }
 }