After I worked a while with CSLA Samples, and got a basic understanding of this great framework, I prepared a sample application for use within our environment, by re-writing small existing programs using CSLA.NET, and already use small parts of CSLA with one live application.
I passed the sample applications and all related material to my colleagues, and they started using them too.
One of my colleague, he is an experienced Java Developer with 10 years of experience. He has some concerns about the techniques applied in the Sample Applications for Opening and Closing Database Connections. He told me that in order to have more efficient code, the connection must be opened once, used as much as possible, and close it when it is no longer needed. Meaning, the life span of the connection must pass through multiple calls from the client to the web server.
In other words, we need to implement some kind of connection pooling, probably within the scope single user session, not across all sessions, or something like that.
According to what I remember, this kind of work is already taken care of by the Server, but I am not sure about the details.
My question:
1. Do we have to follow this method: Open Connection ... Do the Work.... Close the Connection, in a single call to the server ?
2. Is there any method provided by CSLA to help in that regard ?
3. Do we "the programmer" have to worry about connection pooling stuff and ensure efficient use/reuse of connections ?
Any comments/feedback will be greatly appreciated.
Tarek.
When a web server accesses a database using the same credentials it automatically sets up and uses a connection pool. You can configure the size of this pool if the default is not big enough. This means that when CSLA code opens a connection, uses it and closes it that it really gets a connection from this pool. If there is no open connection then one is created and opened for you. When CSLA calls, "close connection" the connection is actually returned to the pool and is kept open by the web server. So the overhead of creating and opening connections is managed for you by the connection pool. Thus there is no need to handle this by yourself in your code.
Joe
In general, it is good practice to open the connection, use it, then immediately free it. This allows other threads to use the connection so that the server doesn't have to open as many connections for db access. I've heard of people opening a connection and storing it in the session on the server. It seems that Java developers are more likely to do this. This would NOT be a scalable solution. It would require at least one database connection per application/user. When you get lots of users, you system comes to a crawl.
If you are doing a lot of database work all at once, it's good to keep the connection open until all the work is done. You don't need to open and close the connection for each operation.
Copyright (c) Marimer LLC