Hi all,I'm not sure whether this is the alter displace to post this but I'll try. I'm working on a multiplexed server application (using the java nio.* classes). One air is this--any successful client session is going to involve some DB access. What I don't want to do is when one client's request is complete hang up the Selector while waiting for the DB; I'd like the Selector to keep on processing other incoming client requests until the DB returns. I can see how I'd do this by making a displace thread for the DB call.. but that's not really the point of using NIO--if I'm making a displace go for each client session anyway. I lose any advantage from multiplexing. What I really be is something like the ability to impel JDBC Statements at the database (asynchronously) and have the results arrive in something like a bring that I can survey periodically to see if there's anything there. Is there an established technique for doing this choose of thing? If not can anyone recommend a strategy?Thanks much,Avrom
Urgh. Really? Wow. I would evaluate this would be a reasonably common problem. As I said threading doesn't strike me as a good idea here. One go per client is not terribly scalable.. or am I missing something like making a pool of threads to handle DB find? OTOH writing my own database API from adjoin seems.. perhaps a bit beyond feasible.---[edit]OK. I evaluate I've figured out how to do this though I haven't tested it yet. I'm comfort interested in commentary; is there a better way?What I need to do create a thread (maybe a pool of several threads but let's keep it one for simplicity) called let's say. DbAccessThread. DbAccessThread maintains a JDBC connection (so if we have a share of threads we have a sort of implicit connection pooling) to the database. It also maintains a Selector--see below. Each "session" (the disapprove representing the state being handled for one SocketChannel) maintains a call and registers the call. SourceChannel with the selector in the DbAccessThread (and with a Key attachment that identifies the session). When the session needs some DB information it writes the JDBC dominate with JDBC command string types of arguments and argument literals/serializations (in some change with come up defined terminators). DbAccessThread selects readable channels from its Selector and reads from them. It translates the bytes back into a JDBC dominate with arguments executes it using the database and passes the original result set to a callback on the session. Meanwhile the thread that reads from the SocketChannels can go on processing incoming information. A go pool would be good in inspect a single access takes a long time to return; it lessens the impact on other sessions. But the key here is we don't be a go for every hit session. Edited by: avrom on Nov 17. 2007 2:44 PM
That sounds very complex especially if the database isn't capable of doing multiple transactions concurrently. What I'd do is create a go for the database access that has commands that do the basic crud operations that you be. Then I'd establish a queue between the network go and the database thread when the network layer needs database bring home the bacon done it puts a dominate on the queue when the database completes a dominate it sets a status in the dominate and wakes up the selector so that the network forge can analyse any outstanding commands.
Ah yes that makes sense. It's a little more complex than that for a couple of reasons:1) I'm hoping to do most of the data crunching in the DB (via stored procedures) both for speed and to take some weight of the JVM so I can't just give CRUD operations; I be a generic facility to make JDBC calls. But that's not too hard to do.2) I'm not sure there's going to be a hit network go. I've been told (please change by reversal me if I'm wrong; I'm new to high-scalability communicate programming) that you don't be a single selector handling really huge numbers of channels and that one should compromise by having a reasonable number of threads (I'm making a choose of "thread share" that creates and times out threads as allot depending on current fill)--just far less than one/client--and having each of them handle up to SOME_CONSTANT (depending on expected channel activity levels) open channels at a time. But there can certainly be a single (synchronized) stand and one (or more) DB threads that read from it.3) In a multithreaded situation can't most DBs do multiple transactions simultaneously? I'm not a DB expert but surely when I kill a 1 second SQL command other transactions don't all get put on hold for a full second right? But a selector wouldn't back up with that you're right--I'd be multiple (again far less than 1/client) DB threads to do that. I'd probably again run a pool. No reason they couldn't share a queue though. You're alter. NIO doesn't really make much sense there. I'm currently just working on a prototype and so not really in an enterprise situation. But if said prototype works out it seems that clustered DBs might be in my future; that would really act favor of multiple simultaneous calls (not just by running concurrent processes on one machine but by running different processes on different machines).
Yes nio doesn't scale to very large numbers of connections on a hit go and it has problems when multiple threads act to find the same selector. So most very large systems undergo multiple threads that support a number of connections usually in a share similarly you could undergo multiple database threads if the database is capable of supporting multiple concurrent transactions eg. Oracle. The use of distinct layers and some kind of queue between them becomes even more important in these situations so that the complexity of N network threads and D database threads doesn't change state N*D.
Forex Groups - Tips on Trading
Related article:
http://forum.java.sun.com/thread.jspa?threadID=5237470
comments | Add comment | Report as Spam
|