Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / September 2004

Tip: Looking for answers? Try searching our database.

serialized access (synchronized)

Thread view: 
Tomer Ben-David - 25 Jul 2004 08:23 GMT
I know synchronized nor any other thread management thingies should be
used in EJB's however.

I have a piece of code in a session bean method that accesses a mail
server.  I must assure that only one thread at a time executes this
method (serializable access), how can I assure that?
Robert Klemme - 26 Jul 2004 12:23 GMT
> I know synchronized nor any other thread management thingies should be
> used in EJB's however.
>
> I have a piece of code in a session bean method that accesses a mail
> server.

Why is that?  SMTP servers can usually handle multiple concurrent
requests.

> I must assure that only one thread at a time executes this
> method (serializable access), how can I assure that?

If you want to access a POP or IMAP account then maybe you can have an EJB
describing the connection and do all communication via this bean guarding
access with a flag as persistent property.

Regards

   robert
Tomer Ben-David - 26 Jul 2004 20:35 GMT
> Why is that?  SMTP servers can usually handle multiple concurrent
> requests.

I have some code that adds a message to the mail server (IMAP).
And I have some other code that gets the count of messages.

If one thread gets the count of messages and another one is adding a
message i would have the wrong number of messages in my hand.

> If you want to access a POP or IMAP account then maybe you can have an EJB
> describing the connection and do all communication via this bean

Thats exactly what I have.

> guarding access with a flag as persistent property.

Even if I do this then

Thread1                     Thread2
if (persistent.isFlag());   if (persisten.isFlag());
getMessagesCount();         addMessage();

And thread 1 gets an inconsistent messages count.

> > I know synchronized nor any other thread management thingies should be
> > used in EJB's however.
[quoted text clipped - 15 lines]
>
>     robert
Robert Klemme - 27 Jul 2004 12:25 GMT
> > Why is that?  SMTP servers can usually handle multiple concurrent
> > requests.
[quoted text clipped - 17 lines]
> if (persistent.isFlag());   if (persisten.isFlag());
> getMessagesCount();         addMessage();

The pattern is rather

// atomic operation that will block
// until the lock is accessible
aquireLock();
try {
 getMessageCount();
}
finally {
 releaseLock();
}

and

aquireLock();
try {
 addMessage();
}
finally {
 releaseLock();
}

Regards

   robert

> And thread 1 gets an inconsistent messages count.
>
[quoted text clipped - 12 lines]
> > If you want to access a POP or IMAP account then maybe you can have an EJB
> > describing the connection and do all communication via this bean
guarding
> > access with a flag as persistent property.
> >
> > Regards
> >
> >     robert
Tomer Ben-David - 27 Jul 2004 19:36 GMT
Is there a possibility to implement aquireLock() method without using
the java synchronized keyword?

> > > Why is that?  SMTP servers can usually handle multiple concurrent
> > > requests.
[quoted text clipped - 71 lines]
> > >
> > >     robert
Robert Klemme - 29 Jul 2004 10:59 GMT
Possibly not.  But maybe you can declare the method or the bean on EJB
config level as not reentrant.

   robert

> Is there a possibility to implement aquireLock() method without using
> the java synchronized keyword?
[quoted text clipped - 74 lines]
> > > >
> > > >     robert
Tomer Ben-David - 29 Jul 2004 15:43 GMT
And what if I do want my bean to be reentrant...

It seems to me like this is a general problem that requires a general
solution.  Some "design pattern" that says :

Requirement : You need to have some of your code to be single threaded
and you have that piece of code in a session bean .

General Solution : Thats what im looking for ;)

> Possibly not.  But maybe you can declare the method or the bean on EJB
> config level as not reentrant.
[quoted text clipped - 88 lines]
> > > > >
> > > > >     robert
W.Guerlich - 05 Aug 2004 12:43 GMT
tomerbd@barak-online.net (Tomer Ben-David) wrote in message
> Is there a possibility to implement aquireLock() method without using
> the java synchronized keyword?

I don't think avoiding the synchronized keyword at all cost isn't
really what the EJB calls for. Otherwise it wouldn't be possible to
use any class inside an EJB that uses that keyword (e.g. IO functions,
Hashtable).
What the specification really means is that the developer doesn't have
to care about multithreading in *general*. Competing for shared
resources among threads is o.k. I would think.

I can't see where the following implementation violates the
specification.
Of course locks like that will only work for EJB instances living in
the same classloader space.

private static Object lock=new Object();
private static boolean locked=false;

private static void aquireLock()
  {
  synchronized(lock)
     {
     while(locked) lock.wait();
     locked=true;
     }
  }

private static void releaseLock()
  {  
  synchronized(lock)
     {
     locked=false;
     lock.notify();
     }
  }
Mark McDowell - 06 Aug 2004 04:07 GMT
> > Why is that?  SMTP servers can usually handle multiple concurrent
> > requests.
[quoted text clipped - 39 lines]
> >
> >     robert

Is it possible to use a message driven bean for the "sent" message, and
subscribe to a JMS topic for the updated message count?

Mark
Tomer Ben-David - 10 Aug 2004 15:07 GMT
> > > Why is that?  SMTP servers can usually handle multiple concurrent
> > > requests.
[quoted text clipped - 44 lines]
>
> Mark

Even if its possible then I'm trying to find a general solution, not
just for the sent messages count but for the general case of a need to
access a resource in a serialized manner from EJB's --> for serialized
access of entity beans to database data in a serialized manner all I
need to do is to mark the transaction with isolation level serialized,
so I'm trying to find something maybe a design pattern for the general
case of trying to access a resource (any resource, in this example it
was a mail server) in a serialized manner. Is there?
Tomer Ben-David - 27 Aug 2004 09:40 GMT
> > > > Why is that?  SMTP servers can usually handle multiple concurrent
> > > > requests.
[quoted text clipped - 53 lines]
> case of trying to access a resource (any resource, in this example it
> was a mail server) in a serialized manner. Is there?

Hello ? I suspect this is an answerable question... (Has no solution
in the specs etc) Is that so ?
ron - 29 Aug 2004 09:54 GMT
> Hello ? I suspect this is an answerable question... (Has no solution
> in the specs etc) Is that so ?

You would have to synchronize the access. Something like

synchronize {
    get the count code
    }
-----
synchronize {
    add the message
    }

Signature

In remembrance -- http://www.ronsmits.org

Tomer Ben-David - 21 Sep 2004 20:49 GMT
> > Hello ? I suspect this is an answerable question... (Has no solution
> > in the specs etc) Is that so ?
[quoted text clipped - 8 lines]
>     add the message
>     }

But synchronization is something that should not be done in a
container-j2ee environment! so do I have any option ? to synchronize -
I shouldnt because of the j2ee environment, not to synchronize - I
cant my application wont work, another example why it wont work is
because generaly a user in my application can access his mailbox 10
times lets say per second, and if I use a javamail implementation with
a store connection pool of size 5 (for example... and for the sake of
the case) the user will get errors! because he tried to connect with
more than 5 connection at the same time! (And I want to allow the user
to do it...) what can I do ?
tinho023 - 01 Aug 2004 14:20 GMT
> I know synchronized nor any other thread management thingies should be
> used in EJB's however.
>
> I have a piece of code in a session bean method that accesses a mail
> server.  I must assure that only one thread at a time executes this
> method (serializable access), how can I assure that?
The ejb takes care of all that,thats what there for.
Regards;
Tomer Ben-David - 02 Aug 2004 07:10 GMT
> The ejb takes care of all that,thats what there for.

From the EJB Spec (2.0):
"...the EJB
architecture does not define an API for managing isolation level.)..."

More from the section :
17.3.2
"...The isolation level describes the degree to which the access to a
resource manager by a transaction is
isolated from the access to the resource manager by other concurrently
executing transactions.

The API for managing an isolation level is resource-manager specific.
(Therefore, the EJB
architecture does not define an API for managing isolation level.)..."

Therefore I dont see any way in CMTD to control the isolation level of
a stateless session bean method.  What are my options?

> > I know synchronized nor any other thread management thingies should be
> > used in EJB's however.
[quoted text clipped - 4 lines]
> The ejb takes care of all that,thats what there for.
> Regards;
tinho023 - 01 Aug 2004 14:24 GMT
> I know synchronized nor any other thread management thingies should be
> used in EJB's however.
>
> I have a piece of code in a session bean method that accesses a mail
> server.  I must assure that only one thread at a time executes this
> method (serializable access), how can I assure that?
Use cmt ,this will secure that only one threat is executing youre method.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.