1

I am using a thread based approach to poll the status of a specific task on AWS. For this, I use a while loop to constantly check the status as shown in below code. The issue is that when the code switches from one Service to another, it runs into an error -

Could not obtain transaction-synchronised hibernate session  

The function in the thread is as below:

        Runnable task = new Runnable() {
            @Override
            public void run() {
                Session session = null;
                try {
                    session = sessionFactory.openSession();
                    RmlWorkspace rmlWorkspace = session.get(RmlWorkspace.class, id);
                    logger.info("Starting Status check for "+id);
                    if (rmlWorkspace.getCloudStack().getStatus() == RUNNING_STATUS.STARTING) {
                        while (rmlWorkspace.getCloudStack().getStatus() != RUNNING_STATUS.ON) {
                            logger.info("Checking Status for "+id);
                            rmlWorkspace = checkStatus(session, rmlWorkspace);
                            TimeUnit.SECONDS.sleep(5);
                        }
                    } else if (rmlWorkspace.getCloudStack().getStatus() == RUNNING_STATUS.STOPPING) {
                        while (rmlWorkspace.getCloudStack().getStatus() != RUNNING_STATUS.OFF) {
                            Transaction tx = session.beginTransaction();
                            rmlWorkspace = checkStatus(session, rmlWorkspace);
                            tx.commit();
                            TimeUnit.SECONDS.sleep(5);
                        }
                    }
                    session.close();
                } catch (Exception e) {
                    logger.info(e.getMessage());
                    if (session != null)
                        session.close();
                }
            }
};

The checkStatus function tries to call a function inside another class with the annotation @Service. The code meets an error at the below code:

private AssumeRoleResult assumeRole() {
        try {
            BasicAWSCredentials credentials = new BasicAWSCredentials(configAttributeService.getAttribute("aws.iamkey"),
                    configAttributeService.getAttribute("aws.iampass"));
            AWSSecurityTokenService client = AWSSecurityTokenServiceClientBuilder.standard()
                    .withRegion(Regions.US_WEST_2).withCredentials(new AWSStaticCredentialsProvider(credentials))
                    .build();
            AssumeRoleRequest request = new AssumeRoleRequest()
                    .withRoleArn(configAttributeService.getAttribute("aws.assumerole"))
                    .withRoleSessionName(UUID.randomUUID().toString()).withDurationSeconds(900);
            AssumeRoleResult assumeRoleResult = client.assumeRole(request);
            return assumeRoleResult;
        } catch (Exception e) {
            throw e;
        }
    }

The class containing the above function has an annotation @Service("xxx")

Could someone explain the reason for this and how to get this working.

The Cloud Guy
  • 963
  • 1
  • 8
  • 20

0 Answers0