|
-
May 8th, 2006, 09:24 AM
#1
Thread Starter
Hyperactive Member
[1.0/1.1] Trying to find an alternative solution for a coding problem
Hi I have a little situation where I've looking for a better solution. The problem originated where multiple clients are sending messages to a single server program and they have to access one file. Now when the file is in OPEN mode, no other file could access it. So I was getting a problem when one client is using the file and another client tries to use the same file from the server and generated an error. So I used a what I did was to create a while loop structure that worked but I think is inefficient. The pseudo code is:
Code:
boolfound = true
while(boolfound)
{
try
{
File.open();
bookfound = false; // break out of the while loop
}
catch(some exception) {}
}
So I will throw an exception to detect if you're trying to open the file at which point in time the file is already opened. the exception will be thrown, and the while structure will loop. The only way the while structure will break is if the file is opened successfully and the second statement in the try clause executes.
As I said it works fine. However, this solution is not good since it runs up the cpu usage when the while structure is looping. And I'm seeing a lot of problems which could be solved using this but I need a better solution. I was thinking about having some kind of event but I'm still a little confused about having an object wait until another is completed.
So you have two clients, C1 and C2. Both accessing the same server. C1 is currently using a file in the server, C2 is tryin to access the same file. How do I make C2 wait until the file is available again without running up the CPU usage?
Jennifer
-
May 8th, 2006, 09:50 AM
#2
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
Hi Jen,
One thing you could do is use a queue and callback system. Requests for the file from clients are added to the queue on the server and the server processes them one by one. As it gets to each one it notifies the client that made the request that it can now access the file. That means neither server nor client has to loop whilst waiting for the message, instead you could use an event handler.
It still seems a bit clunky though, what do you need the file access for? Maybe there's a neater solution.
-
May 8th, 2006, 10:13 AM
#3
Thread Starter
Hyperactive Member
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
Well its not only a for a file system. I've met a lot of areas in which I've been seeing this develop for e.g. access to the database, there could only be one connection. thus when the server is trying to access the database for one client, and at that point in time it is being accessed by another client, then it has to wait until that connection is freed up since you could only have one connection to the database. I get the part with the event handler. However, I still don't get exactly how to have that object in the server wait until either the file is freed up or the database is freed up.
The only thing with your solution idea is that I don't want the client to know what is happening. If their message to access a file or database has to wait, then that should be handled at the server level. I want all of that to be hidden from the clients.
I was thinking of an event handler structure. But I want something so that for e.g. the database, when the database connection is freed up, it triggers some kind of event that will cause any waiting transactions to go ahead. Again I'm a little fuzzy about the idea cause I don't know how to have these transactions wait except for some kind of while loop or a timer thread. This is such a common problem in a multi threaded environment that I believe there has to be a more efficient solution.
-
May 8th, 2006, 10:21 AM
#4
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
Sure, the client shouldn't see the behind the scenes stuff happening I agree.
Wouldn't it make sense to have such an operation performed purely on the server itself? So for example the client application requests the operation with such and such parameters, it gets queued on the server, and returns immediately to the client. Then when the server gets time it executes the operation from the queue, asynchronously to any clients.
Unless the client app requires a response, in which case you would have to make it wait for a notification from the server. You could also possibly prioritise requests requiring return values in the server queue so that any waiting time on the client side is minimised.
As for the waiting itself you can try to break the routine into multiple procedures so that control can wait on an event being fired without requiring a loop.
Does that help at all?
-
May 8th, 2006, 02:02 PM
#5
PowerPoster
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
are you saying that the clients have to access A file? why dont you just send down the stream the contents of the file, and then let the client establish/write the file? Or just send down the contents of the file to the client?
-
May 11th, 2006, 09:22 AM
#6
Thread Starter
Hyperactive Member
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
I understand you point penegate. Just that I cannot send a reply to the client since the reply must be the message. About the queue, is there a built in queue procedue in c sharp that I could use?
Techo, its either a file or a database connection at the server. The server could only open a file once and read it to a client and also a database connection. Thus if another client sends a request for the server to query something in the database, if the database is opened at that point, an exception will be generated. That's why I put in the while loop, to loop until the database connection is closed and then the query will proceed.
Im trying to find someway to have that request at the server wait until either the database connection is closed or the file connection is closed without having to construct a while loop since it carries up the cpu.
-
May 11th, 2006, 11:08 AM
#7
PowerPoster
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
why dont you use SQL Server to store the informtion using a proper database system? much better over the long term and best practice!
-
May 12th, 2006, 02:00 PM
#8
Thread Starter
Hyperactive Member
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
I dont have the flexibillity as to choose which database. They're using oracle and i have to go with that. It's only that you cannot peform multiple queries on the database so it poses a problem. You could only execute one query at a time and there is where the problem stemmed since it's a multi-threaded environment.
-
May 12th, 2006, 02:37 PM
#9
PowerPoster
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
tut - should be using the great SQL Server then.... when will people learn *rolls eyes*
my 2 cents.... would be interesting to find the solution though...
-
May 13th, 2006, 02:52 AM
#10
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
I don't believe that Orace would be so limited. Something must be wrong in your setup.
The queries are being made from the server at the request of the client right? Not from the client?
-
May 14th, 2006, 04:07 AM
#11
Addicted Member
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
Hi, this problem is quite common,
I would sugest that you look into asynchronous file access, this should provide an effective solution.
If you need any info about this then let me know...
Rohan
You dont need eyes to see, you need vision.
-
May 15th, 2006, 09:09 AM
#12
Thread Starter
Hyperactive Member
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
Please send me some information about a possible solution. Just as I thought, its a common problem. it has to be.
Yes penegate. The queries are being made by the server not the client. All the client does is to request to the server to query to the database.
And I don't think that anything is wrong with my setup. When for e.g. I used MySql, I could run multiple queries at the very same time, even embed a query inside of another one and it will work, but not in Oracle. With Oracle, it only allows 1 connection to the database or you will get an error saying Connection is already opened. I believe it's something with the license of oracle since the more connections you have you will have to aquire additional licenses. You cannot evem embed a query inside of another one. For e.g. if you have to execute 2 queries one after another, you have to open the connection, execute query 1, then close the connection, then re-open it again, execute query 2, then close the connection again. That's how it works.
Oh yeah, and SQL server is a novice database compared to Oracle. Only oracle could handle the huge amount of workload my application requires, so stop rolling your eyes!
-
May 15th, 2006, 09:17 AM
#13
PowerPoster
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
I'm sry but that is the most crap I've heard in my life about sql server. you are very wrong. it has won awards and THE most used database system in the world. sry I don't agree
-
May 15th, 2006, 09:20 AM
#14
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
 Originally Posted by Techno
I'm sry but that is the most crap I've heard in my life about sql server. you are very wrong. it has won awards and THE most used database system in the world. sry I don't agree
Noted.
 Originally Posted by Jennifer
With Oracle, it only allows 1 connection to the database or you will get an error saying Connection is already opened. I believe it's something with the license of oracle since the more connections you have you will have to aquire additional licenses.
Are you doing the queries through the same connection? If they can't be executed simultaneously you can queue them on the server like I mentioned. It will be a little extra wait for the client but at least it should work and the wait should not be long as the overall server processing time would be roughly the same as if they were executed in parallel.
-
May 15th, 2006, 11:45 AM
#15
Thread Starter
Hyperactive Member
Re: [1.0/1.1] Trying to find an alternative solution for a coding problem
I will try you suggestion Penegate and get back to you.
Techno, Oracle is the grand daddy of sql server, sql server is microsoft for christ sake. Anyway I am not getting in an oracle / sql server argument since i use both of them. And watch your language with me. Cause You don't wanna know what a woman's scorn is like.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|