You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Chenming Zhao <zh...@bu.edu> on 2002/03/25 16:53:37 UTC

Multiple users share java bean?

Hi,
When two users call the bean almost at the same time, it seems that they share the same object and their implementing is dependent with each other (I hope they are independent), and cannot get correct results. Should I configure Tomcat? I think the java bean is no problem. It's urgent. Please help me.

Minger

Re: Multiple users share java bean?

Posted by Chenming Zhao <zh...@bu.edu>.
Thank you, Daniel. You saved me. I synchronied objects before but didn't
synchronized such type as
 public void setName(String name) {}. Thanks again,

Minger

----- Original Message -----
From: "Daniel Hinojosa" <dh...@qwest.net>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Monday, March 25, 2002 11:35 AM
Subject: Re: Multiple users share java bean?


>
>
> Chenming Zhao wrote:
>
> >Hi,
> >When two users call the bean almost at the same time, it seems that they
share the same object and their implementing is dependent with each other (I
hope they are independent), and cannot get correct results. Should I
configure Tomcat? I think the java bean is no problem. It's urgent. Please
help me.
> >
> >Minger
> >
> If it's a shared bean, sycnchronize it.   Make sure that
> all mutators(Setters) are synchronized, and it would be a bad idea to do
> that to the
> accessors (mututors) are synchronized too.  If you have more open than
> private member variables in this bean, make sure they are private.
> e.g.
>
> public void synchronized setName(String name) {
>             .....
> }
>
> --
> Daniel Hinojosa
> Java & XML: Consultant | Developer | Instructor
> P.O. Box 4675
> Albuquerque, NM 87196-4675
> (505) 363-5832
>
>
>
>
>
>
> --
> To unsubscribe:   <ma...@jakarta.apache.org>
> For additional commands: <ma...@jakarta.apache.org>
> Troubles with the list: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Multiple users share java bean?

Posted by Daniel Hinojosa <dh...@qwest.net>.
In that case I recommend a facade bean in order to do this....

For example, doing this.




Chenming Zhao wrote:

>Dniel,
>
>There is still one problem: the value set by the second user2 updated the
>value of the first user1 before finishing user1's work. I paste the code.
>Please take a look.
>
>public class test
>{
>    int numEvents= 5;
>
>    public synchronized int test()
>    {
>        int real=0;
>        int eventCounter=0;
>
>        while(eventCounter<=numEvents)
>        {
>            real=eventCounter;
>
>            // wait for 0.01 second
>            try{wait(10);}
>            catch(InterruptedException e){};
>            eventCounter++;
>        }
>        return real;
>    }
>
>    public synchronized int setNumEvents(int sec)
>    {
>        numEvents= sec;
>    }
>}
>
>For example, I input 200 as the value of numEvents for user1. Before
>finishing user1's task, user2 input 100 as numEvents and  begin his work. I
>hope I can get results 200 and 100 respectively. But I got 100 both.
>
>----- Original Message -----
>From: "Daniel Hinojosa" <dh...@qwest.net>
>To: "Tomcat Users List" <to...@jakarta.apache.org>
>Sent: Monday, March 25, 2002 11:35 AM
>Subject: Re: Multiple users share java bean?
>
>>If it's a shared bean, sycnchronize it.   Make sure that
>>all mutators(Setters) are synchronized, and it would be a bad idea to do
>>that to the
>>accessors (mututors) are synchronized too.  If you have more open than
>>private member variables in this bean, make sure they are private.
>>e.g.
>>
>>public void synchronized setName(String name) {
>>            .....
>>}
>>
>>--
>>Daniel Hinojosa
>>
>
>
>--
>To unsubscribe:   <ma...@jakarta.apache.org>
>For additional commands: <ma...@jakarta.apache.org>
>Troubles with the list: <ma...@jakarta.apache.org>
>
>
>
public  class TestFacade {
    private Test test = null;
    public void synchronized process() {
       if (test == null) test = new Test();
       test.test();
       test.setNumEvents(2);
    }
}


With this I am pretty sure that you can take the synchronized off of 
methods of the Test Object and always use this testFacade before using 
your test object.

Hope that helps

-- 
Daniel Hinojosa
Java & XML: Consultant | Developer | Instructor
P.O. Box 4675
Albuquerque, NM 87196-4675
(505) 363-5832






--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Multiple users share java bean?

Posted by Chenming Zhao <zh...@bu.edu>.
Dniel,

There is still one problem: the value set by the second user2 updated the
value of the first user1 before finishing user1's work. I paste the code.
Please take a look.

public class test
{
    int numEvents= 5;

    public synchronized int test()
    {
        int real=0;
        int eventCounter=0;

        while(eventCounter<=numEvents)
        {
            real=eventCounter;

            // wait for 0.01 second
            try{wait(10);}
            catch(InterruptedException e){};
            eventCounter++;
        }
        return real;
    }

    public synchronized int setNumEvents(int sec)
    {
        numEvents= sec;
    }
}

For example, I input 200 as the value of numEvents for user1. Before
finishing user1's task, user2 input 100 as numEvents and  begin his work. I
hope I can get results 200 and 100 respectively. But I got 100 both.

----- Original Message -----
From: "Daniel Hinojosa" <dh...@qwest.net>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Monday, March 25, 2002 11:35 AM
Subject: Re: Multiple users share java bean?

> If it's a shared bean, sycnchronize it.   Make sure that
> all mutators(Setters) are synchronized, and it would be a bad idea to do
> that to the
> accessors (mututors) are synchronized too.  If you have more open than
> private member variables in this bean, make sure they are private.
> e.g.
>
> public void synchronized setName(String name) {
>             .....
> }
>
> --
> Daniel Hinojosa


--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Multiple users share java bean?

Posted by Daniel Hinojosa <dh...@qwest.net>.

Chenming Zhao wrote:

>Hi,
>When two users call the bean almost at the same time, it seems that they share the same object and their implementing is dependent with each other (I hope they are independent), and cannot get correct results. Should I configure Tomcat? I think the java bean is no problem. It's urgent. Please help me.
>
>Minger
>
If it's a shared bean, sycnchronize it.   Make sure that 
all mutators(Setters) are synchronized, and it would be a bad idea to do 
that to the
accessors (mututors) are synchronized too.  If you have more open than 
private member variables in this bean, make sure they are private.
e.g.

public void synchronized setName(String name) {
            .....
}

-- 
Daniel Hinojosa
Java & XML: Consultant | Developer | Instructor
P.O. Box 4675
Albuquerque, NM 87196-4675
(505) 363-5832






--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>