isoman
isoman

Reputation: 762

Can this code lead to a race condition?

I'm learning struts and I have created a dispatchAction that check for users privileges and then forward to the correct page. This is the code :

public class UserCheck extends DispatchAction {

    private static String role = "";

    public class UserAction extends DispatchAction {


        public ActionForward checPrivileges(ActionMapping mapping, ActionForm form,
                HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServletException {
            boolean isAdmin;
            //check if admin and put outputin in isAdmin
            //check if user is admin
            if (isAdmin) {
                role = "admin";
            } else {
                role = "user";
            }
               //forwared based on role
        }
    }

Can this code lead to a race condition since attribute role is static and shared between all the instance of UserCheck ?

Upvotes: 1

Views: 129

Answers (1)

TLD
TLD

Reputation: 8135

Yes it will lead to a race condition if:
1. You have multiple instances of UserAction.
2. When you only have one instance of UserAction, but checPrivileges is not a synchronous method.

Upvotes: 1

Related Questions