You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "Angel.Figueroa" <af...@updatecom.com> on 2007/11/01 04:20:52 UTC

messageReceived and bussiness logic

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.


Re: messageReceived and bussiness logic

Posted by "Angel.Figueroa" <af...@updatecom.com>.
Maarten,

       Suppose you want to use more that one instance. How you assign an
instance to a IOSession?

Angel Figueroa
 

Maarten Bosteels wrote:
> 
> On 11/2/07, Angel.Figueroa <af...@updatecom.com> wrote:
>>
>>
>> Maarten,
>>
>>      If you have a class that make the connection to the database and
>> process the sql query. Is recommended to has a class instance for each
>> IOSession? How you associated the class instance with the ioseccion?
> 
> 
> No, I would use a stateless singleton: one instance that is used by all
> IoSession's.
> In fact, I would use Spring's JdbcTemplate.
> 
>     Do you have an example that describe the use to the basicdatasource
> with
>> mina.
> 
> 
> Just look at the docs of DBCP. The fact that you use it from an MINA app
> really doesn't matter.
> 
> Maarten
> 
> Angel Figueroa
>>
>>
>>
>> Maarten Bosteels wrote:
>> >
>> > Angel,
>> >
>> > The example uses a DataSource, which essentialy IS a connection pool.
>> > Have a look at
>> >
>> http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html
>> >
>> > Suppose you configure mina with a thread-pool that uses maximum 10
>> > threads,
>> > and you configure
>> > a BasicDataSource to have a fixed size of 10.
>> > Then only 10 connections will be made to the database during the entire
>> > lifecycle of your server,
>> > and a thread should never have to wait for a db-connection to become
>> > available.
>> >
>> > One of our servers has constantly 300 tcp connections or more, but uses
>> > only
>> > 15 db connections.
>> >
>> > Maarten
>> >
>> > On 11/2/07, Angel.Figueroa <af...@updatecom.com> wrote:
>> >>
>> >>
>> >> Maarten,
>> >>
>> >>     In you example. Every time messageReceived is called for a
>> IoSession,
>> >> you make a new connection. The cost for the connection will be high,
>> >> particular for a database connection.
>> >>
>> >> But Suppose the example is change to use a Connection Pool. In that
>> way
>> >> every time a new client is connected to the server, a new connection
>> is
>> >> added to the db connection pool, or reuse a connection that is not
>> >> associated to a IOSession.
>> >>
>> >> How this can me implemented in mina.?
>> >>
>> >> Angel Figueroa
>> >>
>> >>
>> >>
>> >> Maarten Bosteels wrote:
>> >> >
>> >> > On 11/2/07, Angel.Figueroa <af...@updatecom.com> 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 <af...@updatecom.com> 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
>> >> >> .
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >>
>> >> --
>> >> View this message in context:
>> >>
>> http://www.nabble.com/messageReceived-and-bussiness-logic-tf4729241s16868.html#a13546644
>> >> 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#a13547580
>> 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#a13549176
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: messageReceived and bussiness logic

Posted by Maarten Bosteels <mb...@gmail.com>.
On 11/2/07, Angel.Figueroa <af...@updatecom.com> wrote:
>
>
> Maarten,
>
>      If you have a class that make the connection to the database and
> process the sql query. Is recommended to has a class instance for each
> IOSession? How you associated the class instance with the ioseccion?


No, I would use a stateless singleton: one instance that is used by all
IoSession's.
In fact, I would use Spring's JdbcTemplate.

    Do you have an example that describe the use to the basicdatasource with
> mina.


Just look at the docs of DBCP. The fact that you use it from an MINA app
really doesn't matter.

Maarten

Angel Figueroa
>
>
>
> Maarten Bosteels wrote:
> >
> > Angel,
> >
> > The example uses a DataSource, which essentialy IS a connection pool.
> > Have a look at
> >
> http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html
> >
> > Suppose you configure mina with a thread-pool that uses maximum 10
> > threads,
> > and you configure
> > a BasicDataSource to have a fixed size of 10.
> > Then only 10 connections will be made to the database during the entire
> > lifecycle of your server,
> > and a thread should never have to wait for a db-connection to become
> > available.
> >
> > One of our servers has constantly 300 tcp connections or more, but uses
> > only
> > 15 db connections.
> >
> > Maarten
> >
> > On 11/2/07, Angel.Figueroa <af...@updatecom.com> wrote:
> >>
> >>
> >> Maarten,
> >>
> >>     In you example. Every time messageReceived is called for a
> IoSession,
> >> you make a new connection. The cost for the connection will be high,
> >> particular for a database connection.
> >>
> >> But Suppose the example is change to use a Connection Pool. In that way
> >> every time a new client is connected to the server, a new connection is
> >> added to the db connection pool, or reuse a connection that is not
> >> associated to a IOSession.
> >>
> >> How this can me implemented in mina.?
> >>
> >> Angel Figueroa
> >>
> >>
> >>
> >> Maarten Bosteels wrote:
> >> >
> >> > On 11/2/07, Angel.Figueroa <af...@updatecom.com> 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 <af...@updatecom.com> 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
> >> >> .
> >> >>
> >> >>
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >>
> http://www.nabble.com/messageReceived-and-bussiness-logic-tf4729241s16868.html#a13546644
> >> 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#a13547580
> Sent from the Apache MINA Support Forum mailing list archive at Nabble.com
> .
>
>

Re: messageReceived and bussiness logic

Posted by "Angel.Figueroa" <af...@updatecom.com>.
Maarten,

     If you have a class that make the connection to the database and
process the sql query. Is recommended to has a class instance for each
IOSession? How you associated the class instance with the ioseccion?

    Do you have an example that describe the use to the basicdatasource with
mina.

Angel Figueroa
     
 

Maarten Bosteels wrote:
> 
> Angel,
> 
> The example uses a DataSource, which essentialy IS a connection pool.
> Have a look at
> http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html
> 
> Suppose you configure mina with a thread-pool that uses maximum 10
> threads,
> and you configure
> a BasicDataSource to have a fixed size of 10.
> Then only 10 connections will be made to the database during the entire
> lifecycle of your server,
> and a thread should never have to wait for a db-connection to become
> available.
> 
> One of our servers has constantly 300 tcp connections or more, but uses
> only
> 15 db connections.
> 
> Maarten
> 
> On 11/2/07, Angel.Figueroa <af...@updatecom.com> wrote:
>>
>>
>> Maarten,
>>
>>     In you example. Every time messageReceived is called for a IoSession,
>> you make a new connection. The cost for the connection will be high,
>> particular for a database connection.
>>
>> But Suppose the example is change to use a Connection Pool. In that way
>> every time a new client is connected to the server, a new connection is
>> added to the db connection pool, or reuse a connection that is not
>> associated to a IOSession.
>>
>> How this can me implemented in mina.?
>>
>> Angel Figueroa
>>
>>
>>
>> Maarten Bosteels wrote:
>> >
>> > On 11/2/07, Angel.Figueroa <af...@updatecom.com> 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 <af...@updatecom.com> 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
>> >> .
>> >>
>> >>
>> >
>> >
>>
>> --
>> View this message in context:
>> http://www.nabble.com/messageReceived-and-bussiness-logic-tf4729241s16868.html#a13546644
>> 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#a13547580
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: messageReceived and bussiness logic

Posted by Maarten Bosteels <mb...@gmail.com>.
Angel,

The example uses a DataSource, which essentialy IS a connection pool.
Have a look at
http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html

Suppose you configure mina with a thread-pool that uses maximum 10 threads,
and you configure
a BasicDataSource to have a fixed size of 10.
Then only 10 connections will be made to the database during the entire
lifecycle of your server,
and a thread should never have to wait for a db-connection to become
available.

One of our servers has constantly 300 tcp connections or more, but uses only
15 db connections.

Maarten

On 11/2/07, Angel.Figueroa <af...@updatecom.com> wrote:
>
>
> Maarten,
>
>     In you example. Every time messageReceived is called for a IoSession,
> you make a new connection. The cost for the connection will be high,
> particular for a database connection.
>
> But Suppose the example is change to use a Connection Pool. In that way
> every time a new client is connected to the server, a new connection is
> added to the db connection pool, or reuse a connection that is not
> associated to a IOSession.
>
> How this can me implemented in mina.?
>
> Angel Figueroa
>
>
>
> Maarten Bosteels wrote:
> >
> > On 11/2/07, Angel.Figueroa <af...@updatecom.com> 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 <af...@updatecom.com> 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
> >> .
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/messageReceived-and-bussiness-logic-tf4729241s16868.html#a13546644
> Sent from the Apache MINA Support Forum mailing list archive at Nabble.com
> .
>
>

Re: messageReceived and bussiness logic

Posted by "Angel.Figueroa" <af...@updatecom.com>.
Maarten,

    In you example. Every time messageReceived is called for a IoSession,
you make a new connection. The cost for the connection will be high,
particular for a database connection.

But Suppose the example is change to use a Connection Pool. In that way
every time a new client is connected to the server, a new connection is
added to the db connection pool, or reuse a connection that is not
associated to a IOSession.

How this can me implemented in mina.?

Angel Figueroa



Maarten Bosteels wrote:
> 
> On 11/2/07, Angel.Figueroa <af...@updatecom.com> 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 <af...@updatecom.com> 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
>> .
>>
>>
> 
> 

-- 
View this message in context: http://www.nabble.com/messageReceived-and-bussiness-logic-tf4729241s16868.html#a13546644
Sent from the Apache MINA Support Forum mailing list archive at Nabble.com.


Re: messageReceived and bussiness logic

Posted by Maarten Bosteels <mb...@gmail.com>.
On 11/2/07, Angel.Figueroa <af...@updatecom.com> 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 <af...@updatecom.com> 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
> .
>
>

Re: messageReceived and bussiness logic

Posted by "Angel.Figueroa" <af...@updatecom.com>.
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  ?

Angel Figueroa



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 <af...@updatecom.com> 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.


Re: messageReceived and bussiness logic

Posted by Maarten Bosteels <mb...@gmail.com>.
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 <af...@updatecom.com> 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
> .
>
>