Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
F'Nog
Jul 21, 2001

Mill Town posted:

Sounds cool, but I see it takes SQL commands as raw text. Is there a sanitize function available? I can't find one by searching the site or Google. Thanks!
It supplies a JDBC driver doesn't it support PreparedStatements? We use HSQLDB (H2 is another option, they both came about when Hypersonic forked) for the couple of instances we need an embedded database. Derby is an optional bundle with the JDK not the JRE so it's presence in an environment is something you'll have to make certain of anyway. Bundling a DB with the JDK is retarded anyway, it's too transient. I wonder if we'll get Glassfish when version 7 comes out.

Adbot
ADBOT LOVES YOU

zootm
Aug 8, 2006

We used to be better friends.

1337JiveTurkey posted:

Apache Derby claims to be fairly lightweight but I don't have much experience with it.
It can be quite memory hungry from my experience, but my experience is very old. It is a "full" SQL implementation, though, rather than SQLite and the like which are cut down in order to be more lightweight.

Mill Town
Apr 17, 2006

F'Nog posted:

It supplies a JDBC driver doesn't it support PreparedStatements? We use HSQLDB (H2 is another option, they both came about when Hypersonic forked) for the couple of instances we need an embedded database. Derby is an optional bundle with the JDK not the JRE so it's presence in an environment is something you'll have to make certain of anyway. Bundling a DB with the JDK is retarded anyway, it's too transient. I wonder if we'll get Glassfish when version 7 comes out.

See, that's the kind of information I was looking for. I didn't know about PreparedStatements. I was just googling "java sanitize database" and the like and it didn't come up. Thanks!

SuperGoon
Nov 13, 2003

GT_Onizuka posted:

Recommendations for a Java memory profiler? I don't want to attach it to any IDE as I need to run it all remotely. We're using Java 1.5.

I have used JProfiler, and it is great, but it is not free. Since you are on java 5 why not try setting up an MBean interface and then remotely connecting to that via jconsole. That provides some remote monitoring functionality.

adante
Sep 18, 2003
hi, I want to readLine() from multiple BufferedReaders and do the same thing with the result (after some transforms, depending on the specific BufferedReader instance.

Is there a better way of doing it than instantiating a thread for each reader?

e.g. what I do now (this code is just written of the top of my head, but hopefully illustrates the point):
code:
for (BufferedReader br : listOfReaders)
{
	new Thread() {
		public void run() {
			String s = br.readLine()
			// stuff here
			DoThing(s);
		}
	}.start();
}
DoThing() is expected to do its business quite quickly so having inputs pile up is not a big issue.

FWIW the BufferedReaders are based on InputStreams. I was thinking of trying to do something with the InputStream#available() method but I can't really think of anything obvious (un-obvious: re-implementing BufferedReader functionality in some sort of horrific BufferedReaderMultiplex?)

adante fucked around with this message at 20:28 on Aug 22, 2008

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!
Working on a Java web project with servlets, and getting some really strange behavior. A bit of sample code works when run from a main() method, regardless of whether it's in a servlet class or a back-end class. When the same code is actually called in a servlet from the webpage, it fails.

What this code does is connect to a secure webservice using client/server certificates for authentication, sends a query, and returns the result. In the case where this fails, I'm pretty sure it's because the client certificate isn't being sent properly (though the error message is vague, and it's possibly something else) - javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

Is there any reason why identical code could behave differently in these two contexts?

Incoherence
May 22, 2004

POYO AND TEAR

adante posted:

hi, I want to readLine() from multiple BufferedReaders and do the same thing with the result (after some transforms, depending on the specific BufferedReader instance.

Is there a better way of doing it than instantiating a thread for each reader?
Depends how many readers you have relative to how long it takes for each one. If you have, say, a whole lot of readers, but for each one all you want to do is read a line, munge it a bit, and move on, the cost of starting that many threads may become a nontrivial portion of your runtime, at which point thread pools (java.util.concurrent.something) become an option.

This article is old, but explains what I just said in more detail: http://www.ibm.com/developerworks/library/j-jtp0730.html

covener
Jan 10, 2004

You know, for kids!

Kilson posted:

Working on a Java web project with servlets, and getting some really strange behavior. A bit of sample code works when run from a main() method, regardless of whether it's in a servlet class or a back-end class. When the same code is actually called in a servlet from the webpage, it fails.

What this code does is connect to a secure webservice using client/server certificates for authentication, sends a query, and returns the result. In the case where this fails, I'm pretty sure it's because the client certificate isn't being sent properly (though the error message is vague, and it's possibly something else) - javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure

Is there any reason why identical code could behave differently in these two contexts?
this part of the handshake is unencrypted, so you can see it on the wire. of try -Djava.security.debug=ALL

adante
Sep 18, 2003

Incoherence posted:

Depends how many readers you have relative to how long it takes for each one. If you have, say, a whole lot of readers, but for each one all you want to do is read a line, munge it a bit, and move on, the cost of starting that many threads may become a nontrivial portion of your runtime, at which point thread pools (java.util.concurrent.something) become an option.

This article is old, but explains what I just said in more detail: http://www.ibm.com/developerworks/library/j-jtp0730.html

Right, I'm familiar with thread pools to an extent but my question was more: how do I do it when readLine() is a blocking operation and there is (as far as I can see) no non-blocking alternative?

If you can provide some example code that, in a single thread, could check multiple BufferedReader#readLine() calls and service them/add them to a worker pool, I'd be interested in seeing it.

Incoherence
May 22, 2004

POYO AND TEAR

adante posted:

Right, I'm familiar with thread pools to an extent but my question was more: how do I do it when readLine() is a blocking operation and there is (as far as I can see) no non-blocking alternative?
The example you give calls readLine() within a thread, so thread pools don't really change that problem. I didn't get the impression from your post that that was your big problem, but maybe I wasn't reading it closely enough. If the readers are all reading from the same place (disk, network), they're probably going to be queued by something anyway. (I'm pretty sure hard disks can't read 500 different files at once. Could be wrong, of course.) You may as well have threads processing while they're waiting for I/O, and you may as well use a thread pool since you really don't need to start up and tear down N threads just to keep busy while you wait for N readLine calls.

Basically, the only change I suggested is that instead of starting each "read, DoThing" task in its own thread, as your example does, you'd start each in a thread pool, which would have approximately the same result but wouldn't need to set up quite so many threads.

adante
Sep 18, 2003

Incoherence posted:

The example you give calls readLine() within a thread, so thread pools don't really change that problem. I didn't get the impression from your post that that was your big problem, but maybe I wasn't reading it closely enough. If the readers are all reading from the same place (disk, network), they're probably going to be queued by something anyway. (I'm pretty sure hard disks can't read 500 different files at once. Could be wrong, of course.) You may as well have threads processing while they're waiting for I/O, and you may as well use a thread pool since you really don't need to start up and tear down N threads just to keep busy while you wait for N readLine calls.

Basically, the only change I suggested is that instead of starting each "read, DoThing" task in its own thread, as your example does, you'd start each in a thread pool, which would have approximately the same result but wouldn't need to set up quite so many threads.

right, and what I meant is, I don't know how to do this. I don't see how it is possible to have N threads servicing M BufferedReaders where N < M. Maybe there is a misunderstanding here, but I think code speaks for itself. If you could provide some code/pseudocode that illustrates how to do this I'd be really interested.

Incoherence
May 22, 2004

POYO AND TEAR

adante posted:

right, and what I meant is, I don't know how to do this. I don't see how it is possible to have N threads servicing M BufferedReaders where N < M. Maybe there is a misunderstanding here, but I think code speaks for itself. If you could provide some code/pseudocode that illustrates how to do this I'd be really interested.
That's kind of the idea behind a thread pool. Instead of creating M threads that do one task apiece (as your example does), you create a thread pool with N threads, and you add your M tasks to the thread pool. The thread pool is usually implemented as a producer/consumer setup: the thread pool controller has a queue of work to do, and as worker threads finish what they were doing before they get work from the queue.

In particular, what I think you want is this. This code sample is pretty close to what you're doing, I guess.

oh no computer
May 27, 2003

code:
// Finds the nth fibonacci number
public BigInteger fib(int n)
{
	BigInteger fib1 = BigInteger.ONE;
	BigInteger fib2 = BigInteger.ONE;
	BigInteger temp;
		
	for (int i = 2 ; i < n ; i++)
	{
		temp = fib1;
		fib1 = fib2;
		fib2 = fib1.add(temp);			
	}
		
	return fib2;
}
Works fine while n is less than ~19600, but over this it wont work. I've just installed Eclipse (rather than Netbeans, which I'm used to) and it's not printing any errors in the console window, it just tries to run for a couple of seconds and says "terminated". Is it running out of memory or something? I swear I tried this exact same implementation a few months ago and got it well over a million.

oh no computer fucked around with this message at 14:11 on Aug 23, 2008

sonic bed head
Dec 18, 2003

this is naturual, baby!
I don't know why at all but as soon as I added
code:
out.println(fib2);
after the add(temp) statement, it started working. Before that, it kept saying "terminated" as you said. I don't know what the problem here could be, I'll try doing some debugging, but it seems to just go through the loop completely without actually executing any lines inside.

oh no computer
May 27, 2003

Ahh, it just seems to be a problem with writing stuff that big onto the console I think. I just created a text file and used a FileWriter to write the result to that and it works fine for much larger numbers.

adante
Sep 18, 2003

Incoherence posted:

That's kind of the idea behind a thread pool. Instead of creating M threads that do one task apiece (as your example does), you create a thread pool with N threads, and you add your M tasks to the thread pool. The thread pool is usually implemented as a producer/consumer setup: the thread pool controller has a queue of work to do, and as worker threads finish what they were doing before they get work from the queue.

In particular, what I think you want is this. This code sample is pretty close to what you're doing, I guess.

lol we're just going around in circles. I keep saying I don't know how to create a clean solution that services M BufferedReader's using < M threads because readLine() can block and there's no way to tell if it will; and you keep saying "use thread pools!".

Tell you what, if you can provide a solution (M BufferedReaders, <M threads) and implement it in this function, I'll paypal you $15.

code:
import java.io.*;

public abstract class Callback {

	public static void bindCallback(final BufferedReader[] brs, final Callback callback) {
	//implement me!
	}

	public abstract void service(String string);

}

FWIW I have a 1-thread solution which uses BufferedReader#ready() and semi-busy waiting but this does not work properly when the BufferedReader fills with data without a newline, so it doesn't count.

hey wiz
Jun 18, 2005

I think you are missing the point adante. If you use a queue, even though you have multiple threads, it's only going to allow one thread to DoStuff() at a time, so you don't have to worry about readLine() blocking. To reiterate what Incoherence said, thread pools and a queue definately sound like the way to go.

the onion wizard
Apr 14, 2004

adante, I'm fairly sure you'll need to create M readers, however you can service them with less than M threads.

I whipped this up for my own understanding, it might help point you in the right direction.

wlievens
Nov 26, 2006
I'm using a MySql database with the jdbc connector. I get data truncation exceptions (or just truncated data if I set the jdbc compliant truncation to false) when ever I use a string of 32768 or more non-ascii characters as parameter in a PreparedSatement. The field it's writing for is a LONGTEXT field, which should be able to handle 4 billion bytes. I've tried PreparedStatement.setString and PreparedStatement.setCharacterStream, both give me the lovely data truncation error. The problem does not occur when I use an ascii-only string of more than 32k characters.

Any ideas?

adante
Sep 18, 2003

triplekungfu posted:

adante, I'm fairly sure you'll need to create M readers, however you can service them with less than M threads.

I whipped this up for my own understanding, it might help point you in the right direction.

if you mean I'll need to create M reader threads, then yes I agree. Otherwise.. well I still agree but you are reiterating the problem spec :)

hey wiz posted:

I think you are missing the point adante. If you use a queue, even though you have multiple threads, it's only going to allow one thread to DoStuff() at a time, so you don't have to worry about readLine() blocking. To reiterate what Incoherence said, thread pools and a queue definately sound like the way to go.

Ok, MY point is that you guys keep suggesting I use a threadpool. Yes, a N-thread threadpool means my M threads will be very simple and the servicing goes into the N threads, but now I have M + N threads. This is not less than M threads. This is more than M threads! To re-iterate, my original question was if I could do it with less than M threads (well literally, 'Is there a better way of doing it than instantiating a thread for each reader?').

Either you are not answering my question, or you guys genuinely believe this can be done with less than M threads.

If the former, that is not so helpful.

If the latter then this argument is fast becoming academic. I will open up the $15 offer to anybody (paying out first correct solution only) so prove your point by implementing this and earn some easy cash. Rules apply (solution must be 'correct', use <M threads, no insane reflection tricks).

Incoherence
May 22, 2004

POYO AND TEAR

adante posted:

Ok, MY point is that you guys keep suggesting I use a threadpool. Yes, a N-thread threadpool means my M threads will be very simple and the servicing goes into the N threads, but now I have M + N threads. This is not less than M threads. This is more than M threads! To re-iterate, my original question was if I could do it with less than M threads (well literally, 'Is there a better way of doing it than instantiating a thread for each reader?').
Let's back up a second. You could, if you want, instantiate M readers in one thread. If you're looking at a threaded solution at all, this is obviously a step back, but I just want to make it clear that it's possible. So I'm not clear on why you think you would need M + N threads in any version of a thread pool. You need M readers (which have nothing to do with threads), and N threads.

Here's an example (borrowing heavily from triplekungfu, and probably not working):
code:
public class Dongs {
  public void doTheThingWithTheStuff(int numThreads) {
    List<BufferedReader> readers = makeSomeReadersSomehow();
    ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
    // there needs to be a final in here somewhere because we're passing this to
    // an anonymous inner class, but I don't want to have to make an example that
    // compiles so you'll have to figure out exactly where it goes
    for (final BufferedReader reader : readers) {
      threadPool.execute(new Runnable() {
        public void run() {
          String s = br.readLine()
          // stuff here
          DoThing(s);
        }
      });
    }
    threadPool.shutdown();
  }
}
So, you have M readers (however many are created by makeSomeReadersSomehow()), and N threads (numThreads). Not M+N threads. I'm not sure where M+N came from.

zootm
Aug 8, 2006

We used to be better friends.
The non-blocking IO stuff in java.nio lets you deal with many inputs using very few Java threads. You stated before that you knew of no non-blocking alternative so there it is. It's not on Reader because Reader etc. predate all the non-blocking stuff they added in I think version 5. But really I'm kind of lost trying to figure out what all this bickering is about.

For what it's worth an N thread pool to service M readers is still only N threads (Readers do not spin up their own threads - what would be the point?). It will only service N Readers at the same time, though, which kind of defeats the purpose.

zootm fucked around with this message at 22:16 on Aug 25, 2008

adante
Sep 18, 2003

zootm posted:

The non-blocking IO stuff in java.nio lets you deal with many inputs using very few Java threads. You stated before that you knew of no non-blocking alternative so there it is. It's not on Reader because Reader etc. predate all the non-blocking stuff they added in I think version 5. But really I'm kind of lost trying to figure out what all this bickering is about.

here's the cliff's notes from my point of view:

adante: I want to service M BufferedReaders using less than M threads [I do not explicitly state that I want it to work properly - as in all BufferedReaders being serviced regardless of input - but I kind of hoped it would be implied]

Incoherence(after some back and forth): Use threadpools!

hey wiz: Use threadpools!

triplekungfu: [I think he is saying you need M threads but I'm not sure] + Here is some threadpool code!

zootm: a threadpool with less than M threads will not service M BufferedReaders

Anyway, at this stage one persons opinion will do for me. let's consider the discussion closed. For pedagogic reasons I will keep the $15 offer open (and even provide a quick and dirty test/example) if you can re-implement bindCallback to use, say, 3 threads -- so go for it team threadpool, easy money to be made.

adante fucked around with this message at 01:04 on Aug 26, 2008

rawstorm
May 10, 2008

by Ozma
Is it possible to utilize your graphics card in Java?

Entheogen
Aug 30, 2004

by Fragmaster

rawstorm posted:

Is it possible to utilize your graphics card in Java?

Yes. There are OpenGL bindings for Java, that are cross-platform (that is its released for all major OS like windows, MacOS, Solaris and Linux). I am making web-start application in java right now, that utilizes JOGL (Java OpenGL).

There are also more high level packages such as Java3D, and some other ones I forget. JOGL seems to be the most cross-platform one tho, and also fun to do if you already know some OpenGL.

Here is link to their main page: https://jogl.dev.java.net/

If you decide to pick this up, feel free to ask me more questions through PM. I have a bunch of example code I did, and I will be glad to help.

rawstorm
May 10, 2008

by Ozma
I made my own 3D engine and performance drops when I have too many textures. I know that the performance becomes low because there are too many points to rotate and translate so all I need is a way to make the graphics card do the rotations and translations to take some load off the CPU.

the onion wizard
Apr 14, 2004

adante posted:

if you mean I'll need to create M reader threads, then yes I agree. Otherwise.. well I still agree but you are reiterating the problem spec :)

No, that's not what I meant, in the example I gave earlier I had three threads servicing 15 objects.

I got bored and whipped this up. There's 5 threads servicing brs.length BufferedReaders.

zootm
Aug 8, 2006

We used to be better friends.

triplekungfu posted:

No, that's not what I meant, in the example I gave earlier I had three threads servicing 15 objects.

I got bored and whipped this up. There's 5 threads servicing brs.length BufferedReaders.
To be fair if you put them all in a list and iterate over them you can service any number of BufferedReaders with a single thread, can't you? A threadpool of size 5 will at most be servicing 5 BufferedReaders at one time. It runs one task per thread at any one time and it cannot start another until one terminates if it is already using all of its threads (try this for an example using sleeps rather than Readers since I'm lazy). I think the question was about servicing several inputs at once without spinning up a service thread for each, in which case java.nio is probably the right answer.

zootm fucked around with this message at 14:09 on Aug 26, 2008

Entheogen
Aug 30, 2004

by Fragmaster

rawstorm posted:

I made my own 3D engine and performance drops when I have too many textures. I know that the performance becomes low because there are too many points to rotate and translate so all I need is a way to make the graphics card do the rotations and translations to take some load off the CPU.

Is all your rendering done in software on CPU then? It is not easy to make the graphic cards do these calculations and then read them back. You would be better off making your renderer in OpenGL or Direc3D. The graphics card is good at doing mass matrix multiplies on all vectors that you send to it, but it was primarily designed as part of rendering pipeline.

Perhaps you can use some GPGPU to do this, like CUDA, but I have no idea if that is possible on Java.

I am not quite sure about this, but aren't there certain vector operations that CPU can do nowadays? You can make a C++/Assembly program to perform operations on matrices and vectors and use JNI to interface java with that program. You could also use CUDA this way I guess, but it would be pain in the rear end to do all that yourself.

adante
Sep 18, 2003

triplekungfu posted:

No, that's not what I meant, in the example I gave earlier I had three threads servicing 15 objects.

I got bored and whipped this up. There's 5 threads servicing brs.length BufferedReaders.

looks like 5 threads performing a SINGLE service of 15 BufferedReaders, once each. It also looks like if the first 5 BufferedReaders are not written to, but the remainder are, nothing will ever happen.

Here is some code which demonstrates. Try and run that against my M thread test example to see how it should work. If you write any more code, try it against the test code to see if it works.

the onion wizard
Apr 14, 2004

adante posted:

looks like 5 threads performing a SINGLE service of 15 BufferedReaders, once each. It also looks like if the first 5 BufferedReaders are not written to, but the remainder are, nothing will ever happen.

Here is some code which demonstrates. Try and run that against my M thread test example to see how it should work. If you write any more code, try it against the test code to see if it works.

I just took your original example and the code from the ExecutorService api and mashed them together. I didn't realise each reader needed to call service() more than once

If it's possible/likely for the readers to block forever, I agree that this solution isn't going to work. But, to be fair, your original solution with N threads will never terminate either.

Edit: I was pondering this further today, and I'm positive a thread pool won't work if the threads never terminate.

the onion wizard fucked around with this message at 12:39 on Aug 27, 2008

ShinAli
May 2, 2003

The Kid better watch his step.
Quick question about servers and all that. I've made a quick little lightweight Java NIO server for Comet based connections, as I've quickly found out that the "workaround" Comet implementations is pretty unreliable for a high load event distribution. What I'll be doing is packaging the server inside a WAR file and have it deployable inside a real HTTP server like Tomcat/Glassfish/WebLogic/etc. I know those some of those servers already have an NIO if configured properly (Glassfish has grizzly, Tomcat has NIO HTTP protocol), but I want the WAR file to be able to be deployed on any Java server.

I'm not that knowledgeable in server administration, but last time I tried to make a web service endpoint (using Java's lightweight HTTP server) inside of BEA WebLogic, it bitched a whole lot and I ended up not trying to deploy under WebLogic (worked in Glassfish just fine, though). I got around that by not being retarded and made it use the server it's being deployed on as the endpoint, but I need the lightweight NIO server running within the server I'm deploying to. I'm sure Glassfish would have no problem with this, but I'm also sure other servers are a lot more strict (like WebLogic!). Would this be a configuration issue? Should I get around this by running another VM?

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
I have a question about find bugs, it reports a possible NullPointer on the bolded line.

code:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		while (true) {
			String input = null;
			System.out.println("Please enter an expression\n");
			try {
				input = in.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.exit(1);
			}			
[b]			if(input.equalsIgnoreCase("quit") || input.equalsIgnoreCase("exit")){[/b]
				break;
			}
I'm not sure if that's a real warning or just a false positive.

oh no computer
May 27, 2003

MEAT TREAT posted:

I'm not sure if that's a real warning or just a false positive.
It's throwing the error because everything in the if block always gets executed, if an exeception is thrown or not. If an exception is ever thrown, input wont be given a value and thus remains null, but you're going to be calling equalsIgnoreCase() on it anyway.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
The lesson is that everything that depends on input should be in the try block:
code:
try{
  input = in.readLine();
  if(input.equalsIgnoreCase("quit") || input.equalsIgnoreCase("exit"))
    break;
  // ...
}catch (IOException e){
  // ...

Mill Town
Apr 17, 2006

That won't catch a NullPointerException though. You'll have to add a second catch block for that.

oh no computer
May 27, 2003

Mill Town posted:

That won't catch a NullPointerException though. You'll have to add a second catch block for that.
You don't catch unchecked exceptions, you fix your code.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Mustach posted:

The lesson is that everything that depends on input should be in the try block:
code:
try{
  input = in.readLine();
  if(input.equalsIgnoreCase("quit") || input.equalsIgnoreCase("exit"))
    break;
  // ...
}catch (IOException e){
  // ...

Duh. Thank you for clearing that up.

invid
Dec 19, 2002
Hi guys,

I'm trying to pass a form data from a jsp into a java class which will perform some logic before forwarding to another jsp page.

What is the best way to do this?

I know how to pass a form data from a jsp to another jsp file using request.getParameter() but how do I do this to a Java Class?

Adbot
ADBOT LOVES YOU

Mill Town
Apr 17, 2006

BELL END posted:

You don't catch unchecked exceptions, you fix your code.

yeah in retrospect I suppose actually making sure the reference isn't NULL makes more sense.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply