|
Fruit Smoothies posted:While we're on the subject of sockets; I've always prefered async non-blocking. Can someone tell me why I should do sync blocking instead? Should I? I was under the impression that nio was the way to go for performance.
|
# ? Jun 28, 2009 02:16 |
|
|
# ? May 9, 2024 05:45 |
|
tef posted:I was under the impression that nio was the way to go for performance. Yeah, threads are expensive and having a separate one to handle each connection can quickly get out of hand. Though on the client side where you're usually only maintaining one connection blocking IO is plenty good.
|
# ? Jun 28, 2009 04:15 |
|
I feel a lot happier now. gently caress threads most of the time, anyway.
|
# ? Jun 28, 2009 11:49 |
|
Contra Duck posted:Yeah, threads are expensive and having a separate one to handle each connection can quickly get out of hand. Though on the client side where you're usually only maintaining one connection blocking IO is plenty good. Just make sure your blocking socket isn't running on the main thread. Nothing worse for user experience than having a program's user interface freeze up simply because it's waiting for data from the server.
|
# ? Jun 28, 2009 17:55 |
|
Contra Duck posted:It'll look a little different to that example, it'll probably require two threads, but the general principle is the same. You'll have a Selector object and a thread that is listening for connections and adding SocketChannels to the Selector. Then you'll have a second thread that asks the Selector for any Channels that are ready to be read. The SelectionKey determines the operation you're looking for, in that example it's set to ACCEPT but if you use READ then it'll return all the channels ready to be read. Also, I was using serialization with ObjectStreams in order to transfer a special class between client and server which then encoded a message ID and the data. Would I really have to manually encode these objects as byte arrays and then decode them again on the other end in order to make them work here? Is that even feasible? I think I might just try using threads for now, since I would really like to avoid writing thousands of lines of low-level code just to avoid using threads. In that case I'd have another question though - in order to do this with threads I would have to open a socket in the client, constantly check whether there is any new data to read or write and then call the appropriate functions on the stream. I kinda have an idea how to do the writing part, but how do I determine whether there is any new data? I thought InputStream.available() would do that, but apparently not.
|
# ? Jun 28, 2009 22:40 |
|
This is probably a dumb question, but I've been doing some development on Java apps for Blackberry devices using their Java IDE, and I need to tie in some MySQL fetching. Is it actually possible to import the MySQL J connector? I've tried doing this in the IDE and placing the .jar file and/or the com/org subfolders in the classpath, and even in the project directory itself with no success. It seems like the IDE can't import anything outside of the standard rim libraries (com.rim, net.rim, etc). Is this even possible? If not (since the data being pulled is fairly simple) I could just use some sort of php solution, but it'd really be nice to just be able to pull the data directly.
|
# ? Jun 29, 2009 00:55 |
|
Fehler posted:Thanks, but what I don't really get about non-blocking sockets is how I do the reading and writing. From what I read in the docs the only way is to read/write strings encoded as ByteBuffers using the methods from the SocketChannel. But the docs also say that it's not always guaranteed that the entire string will actually be sent/received in one call, and I'm not really sure how to work with that. Fair enough. The stuff in java.nio is very powerful but it is quite difficult to get your head around initially and probably not worth it for a small project. quote:In that case I'd have another question though - in order to do this with threads I would have to open a socket in the client, constantly check whether there is any new data to read or write and then call the appropriate functions on the stream. I kinda have an idea how to do the writing part, but how do I determine whether there is any new data? I thought InputStream.available() would do that, but apparently not. If you're using blocking methods you don't have to concern yourself with figuring out if there's data waiting to be read, just ask for some data and it'll block until it finds it. If you're passing around serialised objects you'll want to use an ObjectInputStream like this: code:
Contra Duck fucked around with this message at 11:41 on Jun 29, 2009 |
# ? Jun 29, 2009 11:39 |
|
Contra Duck posted:
Be careful how do you do this. Usually the UI frameworks are not thread safe. That is: you should not update a UI component from a thread other than the tread who created it. This is true for most (if not all) of them. Here is a tutorial for concurrency in Swing: http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html
|
# ? Jun 29, 2009 12:24 |
|
I am a complete Java neophyte who was thrown into maintaining a big ol' app. Anyhow, I have a method that should grab one row from the database and return one specific column from it. This is all with JDBC. The current implementation is using ResultSet.getInt to get the value of that column - but how do the offsets for getInt work? Do they start at 0 or 1? The JavaDoc doesn't say: code:
It SHOULD be returning 0, but for some reason it's returning 1. code:
Is the getInt index being used correctly?
|
# ? Jun 29, 2009 22:29 |
|
ashgromnies posted:Is the getInt index being used correctly? Yes. code:
|
# ? Jun 29, 2009 23:13 |
|
quadreb posted:Yes. Since it's in a try/finally block wouldn't nothing happen if it did throw an error? That's intended - there should be one row in that table per value of id_column, defaulting at 0. That insert is just so the select on the next line will retrieve a row. I mean, it should throw a duplicate primary key error most of the time, but it's in a try block. That's intended by the original programmer.
|
# ? Jun 30, 2009 15:09 |
|
Have you actually looked at the database and checked what's in there?
|
# ? Jun 30, 2009 15:40 |
|
Mill Town posted:Have you actually looked at the database and checked what's in there? Yes. One row with unique_id = 1, quantity_column = 0 It calls the method on an instance where this.uniqueId == 1
|
# ? Jun 30, 2009 16:22 |
|
return getQuantity(); Does that, perhaps, return 1? Durf, just noticed you said you're not seeing the failed log message.
|
# ? Jun 30, 2009 17:00 |
|
Mill Town posted:
Yeah, it's not getting to the failed line... it's a really weird issue. I'm trying to get a local copy of the application running right now instead of debugging it on the server it's running on. Working with Eclipse is such a pain in the rear end coming from a Perl/Python and vim background.
|
# ? Jun 30, 2009 17:21 |
|
ashgromnies posted:Yeah, it's not getting to the failed line... it's a really weird issue. I'm trying to get a local copy of the application running right now instead of debugging it on the server it's running on. most likely a SQLException. You just need a good old try/catch/finally. In the catch block, the least you can do is : code:
|
# ? Jun 30, 2009 20:08 |
|
rhag posted:The reason is doesn't get to the failed line is because is throwing an exception. I think we all forgot code after an uncaught exception is thrown doesn't run, even if the exception is thrown in a try block. Good call.
|
# ? Jun 30, 2009 20:40 |
|
Hibernate question for everyone. I've got a custom user type in several class. It works fine, the way it's supposed. However, any time I change a model, it does not mark the model as changed (and therefore, does not update the DB), unless another non-custom-type field is changed. Any ideas on this?
|
# ? Jun 30, 2009 22:16 |
|
quadreb posted:I think we all forgot code after an uncaught exception is thrown doesn't run, even if the exception is thrown in a try block. Good call. Yep. Wound up being a really annoying transaction issue - that method winds up being called twice as part of making one call to the application, but it winds up getting a different database transaction each time... so the first time it's called, it is on one transaction, performs the INSERT IGNORE, transaction not committed, then a little later it's still not committed and it calls that method again but with a different transaction this time, and it winds up locking and everything falls to poo poo and it falls through and winds up returning the results of that getQuantity call which will be 1.
|
# ? Jul 1, 2009 00:49 |
|
Randomosity posted:Hibernate question for everyone. I had this issue during development over a year ago. I just had to either manually modify the database or drop the table and let Hibernate recreate it, I never found a way around it.
|
# ? Jul 1, 2009 05:31 |
|
Alright, I think I can clarify the issue. Basically, I've got this class: code:
code:
code:
Now, Hibernate only writes to the DB when it detects that there was a change in the model. It seems like hibernate doesn't detect the custom type as being changed (or, maybe, the List being changed?)? Is there a way I can tell a hibernate model, "No, seriously, you were changed, write this field." EDIT: It seems like, because the reference hasn't changed, Hibernate assumes it's the same thing. I wonder if I should be using Hibernate's list instead of Java.util's. Randomosity fucked around with this message at 18:10 on Jul 1, 2009 |
# ? Jul 1, 2009 17:00 |
|
So there's this other monolithic Java web application we're running. It has a memory leak somewhere, no one has been able to diagnose it exactly but we need to run a nightly restart of the application right now. Anyone have any tips for debugging memory leaks in Java applications or some sort of memory usage profiler that would let me track down what exactly isn't freeing its memory? I have no experience debugging memory leaks and I'm not really sure where to start exactly. The application is freaking enormous and a mess of spaghetti code and FactoryFactory's.
|
# ? Jul 1, 2009 17:17 |
|
ashgromnies posted:Anyone have any tips for debugging memory leaks in Java applications or some sort of memory usage profiler that would let me track down what exactly isn't freeing its memory? I have no experience debugging memory leaks and I'm not really sure where to start exactly. The application is freaking enormous and a mess of spaghetti code and FactoryFactory's. Generally you debug memory leaks by running the program in the profiler, doing a few operations, forcing a GC, and then taking a memory snapshot. Then you look at the largest / most commonly-allocated objects, and you see what's holding a reference to them. Any memory profiler worth the name should be able to do all that. I found YourKit pretty reasonable; at least, it worked well enough for me, so I stopped looking.
|
# ? Jul 1, 2009 17:42 |
|
Randomosity posted:e:removed for brevity You might have to use Hibernate's collection annotations (@OneToMany I think) for Hibernate to realize it needs to handle List internals. That said, I DID just gently caress up the deepCopy() method on an immutable class for a UserType I implemented just last week (in Grails) and it caused Grails to trigger a db save. However, that class was not a List (Grails domain classes and Lists are a disaster). Any particular reason you are using a UserType and not another mapped Entity collection? Don't get me wrong, I use them myself, but the DB I work with is solid poo poo. I'd tend to stay away from them if at all possible. trex eaterofcadrs fucked around with this message at 20:21 on Jul 1, 2009 |
# ? Jul 1, 2009 20:16 |
|
ashgromnies posted:So there's this other monolithic Java web application we're running. JProfiler. It isn't free but you can download a trial version. You can view memory usage over time, active objects, number of instances for each class type, amount of memory consumed by each object, etc. Go to their web page for a full list of features. With the help of JProfiler, I was able to cut the memory usage of an application by about 2/3rds.
|
# ? Jul 1, 2009 20:19 |
|
Ok, let's see... I was handed a little proggie which combs a database, looks for different values (Select distinct), and then replaces them with different correlative numbers. This was done in order to "mask" a database so we lowly offshore code monkeys could not access sensitive information. They realized this approach sucked, and now they want to use different kinds of "mask data" depending on the original data type. So numbers (like phone numbers, ip addresses and such) will still be replaced by 1, 2, 3 and so on, but Strings will have to be replaces by, well, letters. I thought about using randomly generated strings of a fixed length, but they need to be different for every distinct result on the original table, and I think checking every newly generated one to see if it hasn't been already used would be a mess. I was then thinking about an alphanumeric "counter"; is there any way of taking a string (let's say "AAAAAAA" for example's sake), and transform it into "AAAAAAB", "AAAAAAC" and so on and so forth?
|
# ? Jul 1, 2009 20:36 |
|
WalletBeef posted:JProfiler. It isn't free but you can download a trial version. How does it work exactly? Can I run it against a running instance of the application on our server and view the GUI on my local machine? edit: ah ok it has remote profiling... nice. Thanks!
|
# ? Jul 1, 2009 20:57 |
|
Edmond Dantes posted:Ok, let's see... Yeah, you can use BigInteger(String val, int radix); I use this for base-36 (0-9A-Z) conversions.
|
# ? Jul 1, 2009 20:58 |
|
This doesn't deserve its own thread, and I looked through the thread as best I could, but I am planning on learning Java on my own. Can someone suggest a good book to serve as beginner's guide? I have little programming experience, other than a pretty good knowledge of VBA for applications, and I took a C++ class in high school (like 8 years ago). I do work with databases for a living, so I am intimately familiar with SQL and am a quick learner. Basically, I need to know things about Java specifically, not how to turn on the computer or what OOP is. Thanks.
|
# ? Jul 5, 2009 00:08 |
|
Sub Par posted:This doesn't deserve its own thread, and I looked through the thread as best I could, but I am planning on learning Java on my own. Can someone suggest a good book to serve as beginner's guide? I have little programming experience, other than a pretty good knowledge of VBA for applications, and I took a C++ class in high school (like 8 years ago). I do work with databases for a living, so I am intimately familiar with SQL and am a quick learner. Basically, I need to know things about Java specifically, not how to turn on the computer or what OOP is. Thanks. Sun's Java tutorials are always a good place to start - http://java.sun.com/docs/books/tutorial/ If you want a book, here is a list of what was recommended to me at university in my first year. C. Horstmann, Java Concepts, Deitel & Deitel, Java How to Program, Y. Daniel, Introduction to Java Programming, B. Eckel, Thinking in Java (Online version http://www.mindview.net/Books/TIJ/)
|
# ? Jul 5, 2009 12:26 |
|
I've been learning Java on my own, too. First I was looking at the Sun tutorials mentioned above, but I picked up a second hand copy of the Deitel "How to Program" Java book for like £0.50, which is fine and seems to cover most stuff pretty well. Am only about 750 pages into it so far. It's a big-rear end book.
|
# ? Jul 5, 2009 21:18 |
|
Thanks guys. I'll go with How to Program Java.
|
# ? Jul 6, 2009 04:45 |
|
I just wanted to point out another resource that's not in the OP. I was recently talking to a group that works with java and uses Eclipse. I haven't had to look at java since school, but school was about 90% java so back then it was pretty strong, and I've never used Eclipse. So I set about looking for refresher material on both. Eclipse has this series of videos. Its your entire java class from your second year of your CSE degree summarized and done in Eclipse. Its baby stuff but I think he does an awesome job of explaining what hes doing and manages to teach you something about both java and general theory. Just FYI for anyone looking for a starting place with java. Edit: I <3 Eclipse. Bubblegum Wishes fucked around with this message at 01:39 on Jul 15, 2009 |
# ? Jul 14, 2009 23:52 |
|
Edmond Dantes posted:I thought about using randomly generated strings of a fixed length, but they need to be different for every distinct result on the original table, and I think checking every newly generated one to see if it hasn't been already used would be a mess. I was then thinking about an alphanumeric "counter"; is there any way of taking a string (let's say "AAAAAAA" for example's sake), and transform it into "AAAAAAB", "AAAAAAC" and so on and so forth? There is a way to generate unique ID strings. http://java.sun.com/j2se/1.5.0/docs/api/java/util/UUID.html code:
|
# ? Jul 15, 2009 09:36 |
|
Bubblegum Wishes posted:I just wanted to point out another resource that's not in the OP.
|
# ? Jul 15, 2009 13:26 |
|
Is there a simple way to create a list of references to objects? A rough sketch of what I'm trying to do...code:
|
# ? Jul 15, 2009 19:22 |
|
Gravity Pike posted:Is there a simple way to create a list of references to objects? A rough sketch of what I'm trying to do... Yes there is. You see, in Java every object is a pointer. That is, you only work with references, you never work with values. The only time you would ever work with values is when working with primitives (char,boolean,int,double,etc.) Since Java 1.5 one fantastically useful and nice feature has been added to Java: autoboxing. However, for new users this is confusing. For more information about autoboxing read: http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html Now, lets take your code and translate it (for you to see what you have done): code:
The same is true with strings as well: String a="a"; a="b"; The memory location of the first object is different from the memory location of the second one. These objects are ...special in Java. Strings have some operators overloaded, even though Java sdoesnt have operator overloading (concatenate strings with +, while == performs an .equals() ). (One simple catch 22 situation: String extends Object, yet object has a public String toString() method.) So in order to achieve your goal you should have your own object, with a setNeckbeards() method. Create one instance of that object, add it to the vector, set the neckbeards value to something else, and see it change everywhere that object is referenced.
|
# ? Jul 15, 2009 19:39 |
|
Gravity Pike posted:My output's going to be "1 vs 0", when I want it to be "0 vs 0" - I want the list to contain a reference to the variable, not the value of the variable. There is no way of forming a reference to a local variable in Java. Doing so is either unsafe (as in C++) or expensive (as in JavaScript or Smalltalk). Unfortunately, this does make certain kinds of metaprogramming difficult or impossible. If it's an instance variable, you can use closures (anonymous classes) to do sortof what you want, except with gratuitous amounts of syntax: code:
|
# ? Jul 15, 2009 19:54 |
|
rjmccall posted:There is no way of forming a reference to a local variable in Java. What? What does the scope of a variable has to do with references ? What are you talking about? MyNewObject obj=new MyNewObject(); obj is a reference. Global, local, static, final, whatever the hell you want. it is still a reference. int i=1; i will always point to the value of the variable, not to its location in memory. and in java you cannot even get that (like &i in C). rjmccall posted:Doing so is either unsafe (as in C++) or expensive (as in JavaScript or Smalltalk). Unfortunately, this does make certain kinds of metaprogramming difficult or impossible. I have no words for this...
|
# ? Jul 15, 2009 20:12 |
|
|
# ? May 9, 2024 05:45 |
|
Let's get back to the real meat of this thread already: does java pass by value or by reference?
|
# ? Jul 15, 2009 20:35 |