On 11/2/07, Angel.Figueroa <[EMAIL PROTECTED]> wrote:
>
>
> Maarten,
>
> I understand that the messageReceived will never be called
> simultaneously
> for the same session. But it can be called concurrently for different
> IoSession's.
>
> Let put a real example. A Server Application that received SQL Query
> from
> different client connected to it.
> Each client sent a SQL query to the Server. The Server has 10
> connections
> to the database.
>
> How the messageReceived can process each client request (SQL Query)
> simultaneously, using the 10 connections ?
The fact that messageReceived can be called simultaneously (by multiple
threads)
just means that you have to be careful with using shared resources, but
other than that, you don't need to worry.
Since it's reasonable that any DataSource implementation is thread-safe, you
could implement your example like this:
public class QuickMina extends IoHandlerAdapter {
DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void messageReceived(IoSession session, Object message) throws
Exception {
// a TextLineDecoder will ensure that passed in message is a string
String sql = (String) message;
Connection connection = dataSource.getConnection();
try {
Statement stmt = connection.createStatement();
try {
int rowsUpdated = stmt.executeUpdate(sql);
session.write("rows updated: " + rowsUpdated);
} catch(SQLException e) {
session.write("failed: " + e.getMessage());
} finally {
stmt.close();
}
} finally {
connection.close();
}
}
}
Disclaimer:
I haven't tested this code, it's just an example. I wouldn't advise you to
allow clients to send db updates this way :-)
Maarten
>
>
>
> Maarten Bosteels wrote:
> >
> > Angel,
> >
> > Your question is not really clear to me.
> > What exactly are you worried about ?
> > Note that, by default, messageReceived will never be called
> simultaneously
> > for the same session.
> > But it can be called concurrently for different IoSession's, this does
> not
> > pose a problem by itself.
> > You just have to make sure your implementation is thread-safe: don't
> store
> > conversattional state in instance fields of your IoHandler and properly
> > synchronize access to shared data.
> > This is all very analogous to servlet programming.
> >
> > Maarten
> >
> > On 11/1/07, Angel.Figueroa <[EMAIL PROTECTED]> wrote:
> >>
> >>
> >> If more that one message is received at the same time. How to control
> the
> >> execution of the business logic method.
> >>
> >> public void messageReceived(IoSession session, Object message)
> throws
> >> Exception
> >> {
> >>
> >>
> >>
> //----------------------------------------------------------------------
> >> // received the Request Transaction
> >>
> >>
> //----------------------------------------------------------------------
> >> String theMessage = ( String ) message;
> >>
> >>
> //--------------------------------------------------------------
> >> // Process Transaction Code business logic method
> >>
> //--------------------------------------------------------------
> >> theMessage = transaction.ProcessTransaction
> (theMessage,arclient);
> >>
> >>
> >>
> //----------------------------------------------------------------------
> >> // send the response Transaction Back
> >>
> >>
> //----------------------------------------------------------------------
> >> session.write(theMessage);
> >> }
> >>
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/messageReceived-and-bussiness-logic-tf4729241s16868.html#a13522950
> >> Sent from the Apache MINA Support Forum mailing list archive at
> >> Nabble.com
> >> .
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/messageReceived-and-bussiness-logic-tf4729241s16868.html#a13541041
> Sent from the Apache MINA Support Forum mailing list archive at Nabble.com
> .
>
>