Tom Malia

Greenhorn
+ Follow
since Aug 05, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Tom Malia

Thanks,
I was thinking about creating multiple methods but wanted to make sure I wasn't missing a possible language feature that might give the same result. Also, I had some kind of mental block about multiple methods... I had it stuck in my head that the methods would need different names.... don't know why the different signature, by having a Logger parameter didn't dawn on me but I'm certainly grateful to you for making that solution so obvious in hindsight!

Thanks!
8 years ago
I want to write classes with methods that perform JDBC operations that throw SQL exceptions. For many of the methods, I'd ideally like to be able to have them catch exceptions and just send them to a standard Logging system "IF" the code that calls the methods is not going to catch the same exception. However, I'd like the "option" to have code that calls these methods catch the same errors if I want to but not "Require" the calling routines to catch them.... so I don't want to declare the methods with a "throws" that would require all calling code to Try/catch.

For some background, the logic behind what I'm looking to do is that there will be lots of places where these classes and their methods may be used where the code is basically "throw away" scripting code where just having error logs generated is more than sufficient. However there are also places I want to use the same classes/methods that I would want to handle the exception differently. So, for at least half the places I want to use these methods, there's no good reason to require cluttering the calling code with Try/catch, but when I DO want to handle the exceptions, I'd like them to get passed up to the calling routine so I can handle them in a way that is appropriate for the calling routine. Does that make sense?

I guess I'm kind of looking for is the ability to "override" the catch of a called method "IF" I want to but to treat the method as though it doesn't throw any exception "IF" I don't want to override the called routines catch logic.

Is this possible?
8 years ago
Tim,
The project that first prompted this experiment in statics for me, as I said, was not "completely" trivial.... but it was darn close. No one was paying me for it. It was not going to have a very long life at all (needed to work for about a week), only effected at most a couple hundred people in a very minor way....
So.... "unit testing?!" "we don't need no unit testing?!"

However, since each of my functions did one thing and one thing only and didn't rely on anything outside of their local variables and/or the parameters passed in, I was able to test each of them completely independent of any other part of the over all project. So that "blockiness" that was discussed earlier worked out quite well for me.... testing each function was really easy. Once that's done, making sure they played well together when I stacked them was relatively trivial as well.

returning to the tool analogy, my current thinking is that my "staticy" approach, which as others have so pointed out is REALLY a "functional" approach, seems to be a great fit in specific situations. One such situation I believe is problem domains that by nature tend to be stateless.

I've seem the "it's not mockable" argument quite a bit around the web.... I get that. It seems to me that the significance of this argument against static would depend a lot on how completely and efficiently you can test a static function in isolation. Seems to me that, if you can guarantee that a function does what it's defined to do and will always do what it's designed to do, then when, where and why would I ever need to "mock" it in a larger system?

I would say though that, so far this issue with not being able to easily "swap out" statics for something completely trivial without touching the code that actually calls it (If this is "not" the basic idea behind "mocking", I apologies.) for testing purposes is the only fundamental flaw I'm seeing/hearing with "static".

8 years ago
OK, I'm learning a lot. Definitely going to be doing some more reading about functional programming theory.

The other thing I'm hearing is lots of discussion about pros and cons of particular programming paradigms and design philosophies.

What I've still not seen or heard in this discussion is any glaring problem with using a lot of "static" methods in java programming.

Fun food for thought so far and also so far no reason for me to freak out that programs I've written recently may be some kind of ticking time bombs waiting to bite me in the but.

So... far.... so good.
8 years ago
Chris...
Is Scala my holy grail!
I just scanned the first few paragraphs from (http://www.scala-lang.org/what-is-scala.html)!
WOW! Sounds like the kind of thing I've been looking for recently. And It's Java compatible. It looks VERY interesting. I'll have to try to do some reading on that.

Do you happen to know if there's support for in the NetBeans IDE? I can't deny the productivity benefits of a GOOD IDE support for a language with immediate error detection, code complete, refactoring support.... If I can't find an environment that has all that for a language these days, I have that much more trouble justifying a switch to a given language.

8 years ago
I definitely hear what Chris and Bear are saying... I did recognize that I'm basically writing procedural code and "forcing" Java to behave like a procedural language.
Guess I always have to fight the system. In the early 90's I was using pointers to a "this" variable and lots of pointers to functions in C structures because my colleague at the time insisted on NOT using a C++ compiler but I wanted to write OO code, so I had to do it the hard way.

Anyway, I don't know Scala and I'm getting too old to learn new languages. Also, there are times when the standard design models for Java DO work for what I want to do.

SO... to extend the tool analogy, I own a table saw... I don't own a Chop saw... there's lots of times that the table saw is EXACTLY the right tool and there frequently times when a chops saw would be SO much better, BUT I can get the job of the chop saw down with my table saw much of the time.

I definitely appreciate the recommendation that maybe another language would to the things I'm trying to better. But what I'm looking for right now is any reasons I it is just completely wrong that I should try to use Java the way I have. I'm looking for the "Oh CRAP! I didn't realize THAT was going to happen!" kind of thing, because all the other discussions I've seen "imply" that there is or are such major pitfalls to using statics with Java but I've not seen any clearly definitive examples/explanations of what those actually are.

(This is fun :-) Thanks to all for participating! Gotta love the internet... I'd have to search pretty darn hard to find people knowledgeable enough and interested enough to have this kind of discussion any other way!)
8 years ago
Hum.... OK, looks like I have misunderstanding the static thing...
So... what does that mean for static methods of a class where an instance of the class never gets instantiated?
So... the class should load into memory the first time a call is made to either instantiate it or a call is made to one of it's static methods....
Then, that loaded code (and any static variables in the class if any) will stay loaded for the life of the "program".... Hum... so if my class implements a Servelet... then... what's the "program" who's life it persists through? Is it the Web App? Is it the Request? Is it just the duration of the call to the given method?

So... if I assume methods are functionally "variables", the static methods exist for the life of the program... So... what happens to the local variables in the methods? are they functionally static variables themselves? If there are two calls to a single static method at the same time on different threads.... is there just one set of local variables between the two calls and subsequently a HUGE possibility for concurrency problems? Or do instances of all the local variables get created and pushed onto the static when the methods are called so each call, on each thread has it's own variables?

IF, the static class gets loaded once the first time it is referenced.... AND IF method local variables and parameters are all created and pushed onto the static separately for each unique call to a given method....
then I think that's really the ideal situation (so obviously I've got something wrong here) because I only take the performance hit once to load the class, but I still get complete isolation of the code execution.

By design, I do NOT want any of these static methods to effect anything for which I care about the state of the data after the method execution EXCEPT in the case where the method either passes something back as a return OR I intentionally passed a reference to an object in that was intended to be modified by the method. So, IF there's any statefullness needed, it's all dealt with OUTSIDE of and effectively independent of the class the method is part of.
8 years ago
Oh, I just remembered one other reason I "thought" static was a particularly good idea in the Servlet world... I was thinking about scenarios where, for load balancing or something, there might be multiple instances of the Java Application server running... in that scenario, I didn't see a good way to implement the "singleton" pattern since it would require a whole extra layer of architecture to try to implement a "singleton" where there are multiple JVMs, running on multiple servers that by default don't know anything about each other... So, in this scenario, my logic was that if everything is Static and I've done all the work necessary to make sure they work "correctly" as static methods, then it "should" be completely safe for these methods to be called from anywhere any time... they "can't" step on each other.... or at least that was my interpretation of the situation.

As I said.... I'm LOOKING FOR the errors in my ways so please don't misinterpret my response as being "defensive".... I find vigorous debate the best way to get to the truth and that's want I'm really looking for here. Show me where I'm wrong... please.
8 years ago
Why aren't they tread safe as long as now class level variables are being used?
Persist some variable? If you're talking about the SQL back end? If so, that stuff is still effectively "stateless", you read a record to be displayed to the user... you send updates to the records in the database... but I'm not storing data that's then references with in a continuing processing or anything that needs to be "persisted?

What's the performance hit over Singleton? Is it that each time the JVM has to reload the entire class code, as apposed to "spinning up" an instance once, then just queuing up requests to it by other processes? Or is there some other reason it causes performance problems?

As for "lazy", the fact is I sometimes have to work pretty hard to make sure I write static code that works everywhere I need it to, doesn't require an excessive amounts of parameters, CAN'T have nay side effects, etc.. So, if I'm being lazy... them I'm REALLY stupid because I sometimes have to work hard to be that lazy.

Thanks for the feedback. I'm looking forward to learning more about all this.
8 years ago
Ok, the odds that this reference to a 1980 or 90's SNL skit by Jon Lovitz doing his impression of Harvey Fierstien will be completely lost on 90% of the readers of this now and certainly will be COMPLETELY obscure in another 5 to 10 years are pretty high.... but I thought it might garner a laugh or two.

As for the serious nature of the question:
I started getting formal programming training at the dawn of the OO revolution in the mid-80s. At that time I was a zealot that thought everything should be an object. You should always be instantiating an object and storing it's state and calling it's methods, etc. etc. etc..

Recently though, particularly as I start doing more Web application programming, I think I begin to see that just because you have a hammer the world is NOT a nail. (honestly, at my age I figured that out a LONG time ago... but just to make a point...)

I just did a slightly bigger than trivial web application that had a traditional MVC design with a SQL back end, Servlet/JSP controller and Javascript JQuery view.... and I decided to do EVERYTHING as STATIC methods. I create a lot of different classes. Basically a class for each C.R.U.D. function required for each "Entity Type" in my system. So for example there's a GetCustomer class. There's a UpdateCustomer class, there's a DeleteCustomer class, there's a FindCustomer class etc. etc. (debated doing an Entity_CRUD class with methods for each thing... but found code management and things was actually EASIER of they were each separate files all together. This way, each could also be a seperate Servlet performing a designated function. also it allowed me to get one whole FILE working, tested, validated, etc. and then move on to the next). Each of these classes have sets of static methods that perform the functions that basically match the class names, with different possible paramaters, for example, one method might take a CUSTOMER OBJECT as a parameter, another might take a JSON string. One might take a Servlet Context parameter and retrieve a data source object from a pool in the context and another might take a Datasource Parameter directly, etc..

In all cases, the classes have STRICTLY static methods and no class level variables of any kind. All the methods use strictly the parameters passed to them and/or the local variables declared in them. So, as my possibly naive understanding of Java would interpret this, all my methods basically operate completely on the stack within single threads so I don't "think" there's any chance of side effects or parallel processing issues.

As I understand it, this means that each of my methods do their things when their asked without needing to know about or be able to interfere with anything else that's going on anywhere, then everything about them goes away as it all gets popped off the stack.

This all seemed to be quite consistent and compatible with the stateless nature of standard Web application processing, and just generally seemed "neat and tidy" to me.

After implementing this solution for a web app, I started taking the same approach for a lot of other "little programs" that I have been writing that do things like analyze directory structures and the files in them to extract information about program source code or to do file manipulations (basically the kinds of stuff Unix and linux focus traditionally write BIN and BASH scripts for).

All this has worked out so well for me that I'm become a real fan of "ALL STATIC, ALL THE TIME" programming for a lot of scenarios (obviously this is an over statement.... I'd didn't just get a new hammer, I understand the value of a "tool chest" with lots of tools, but boy, an awful lot of place... static seems to work better than non static for me these days).

BUT.... Every where I look in the internet and in a lot of books, I find warnings, cautions and even out right condemnations of the use of STATIC as something at least less than desirable or professional and frequently as evil as.... I don't know what.

So....
Is there some big piece of the puzzle that I'm missing? Are Statics' Really evil? Am I completely nuts for thinking that actually TRYING to make some programs or systems of programs completely static is REALLY DUMB?

Just curious what real experts with LOTs of years of Java programming might think about this.

Thanks.
8 years ago
I'm an idiot!
I could have sworn I had triple checked to make sure that the mysql JDBC jar was in the LIB directory for the tomcat install.... but apparently I didn't.
Copied it there and now that part is working.

8 years ago
By the way... I know I started this talking about connecting to an access database and now I'm trying to connect to MySQL... the fact is, my web app needs to connect to both a MySQL and and Access database.
Just wanted to clarify.
8 years ago

E Armitage wrote:But the method is there in the API http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/tomcat/jdbc/pool/DataSourceProxy.html#setPoolProperties(org.apache.tomcat.jdbc.pool.PoolConfiguration)

Is that the exact code you are using or are you perhaps using javax.sql.DataSource?



Ah! You're right....

changing:
DataSource datasource = new org.apache.tomcat.jdbc.pool.DataSource();
to:
org.apache.tomcat.jdbc.pool.DataSource datasource = new org.apache.tomcat.jdbc.pool.DataSource();

Got me passed the problem with setting the properties. Thanks.

However... new problem....

when it executes:
connection = datasource.getConnection();

I get: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I know the MySQL drivers are in the project. They are also in the LIB directory for the Tomcat Install.

I can get the connection just fine if I just load the driver myself and create a standard connection.

The following works:



But the following here fails with the classNotFoundException:

8 years ago
The code I posted earlier on for creating a connection pool.... I got that straight from the Apache Tomcat 7 website (The Tomcat JDBC Connection Pool) So I figured it should work....

However, that code calls a "setPoolProperties(p)" method on the dataSource object. The problem is, there's no such method. In fact, I did a Google search of the string "SetPoolProperties" and the only hit I get is that same page from the Apache Tomcat project website....
What's the scoop? Is there a way to define the Pool properties and then set a data source with those properties?

By the way. Thank goodness for the internet in general and you guys here in particular! Though I don't consider myself a geezer yet, my programming days do go back far enough that I have painful memories of spending hours in the book stored debating which of the $100+, multi-thousand page tomes on my current language of choice might answer my questions when I get home. Only to find myself back the book store again a few days later trying to find another book to get me through the next dead end I ended up in. My GOD, it's amazing how much more productive programming is when there's literally a world wide community willing to listen to questions and try to help! THANKS!

8 years ago
Regarding the use of Access.... ya I know. Not a great database engine for anything other than one user, local stuff. But like I said and as you noticed, I don't really have control over that.

Regarding:

Also, production setups usually mean that those databases are not infinite and going to come from unknown sources.

: In this case actually production setup means exactly that the number of possible database are infinite and the location are unknown. The user of the Race management software can create any new databases they want, anywhere they want. Think of it like someone going into Excel and just creating an new workbook (xls) file. The one significant difference from the Excel example is that the race management program always creates databases with the exact same schema structure which is why I can reliably connect to any database it creates and know I can read it.

Regarding:

So that would imply the process of downloading and installing both Tomcat and your web application.

:Yep... that's pretty much right.

A little more pertinent background.... given that NOTHING I'm trying to do would be recommended procedure for an serious web application.... This is REALLY small time stuff I'm doing here. For example, right now all I'm trying to do is make a Cub Scout Pinewood Derby event for my son's scout pack a little more fun. Eventually I might try doing the same kind of thing for my kid's Swim meets which are quite small events. So, these web apps will need to run for maybe a few hours (ok, if you've got kids on swim teams you now "a couple hours" is optimistic, but it's still not days or week... they only SEEM like they are taking days and weeks) and will not be accessed by more than maybe 100 people maximum at a time.

The only reason I want to do this whole thing with the "Administration page" for selecting the database source is because next year when I'm trying to do this all again for the next Pinewood Derby race, I'll probably have forgotten everything I taught myself about how to setup the connection pools in the Tomcat XML files. So, I want to make it easy for me to just deploy the WAR, and then get sent straight to the configuration page that let's me tell the app where this years race database is located to get it all setup again.

Thanks so much to all for all your very helpful advise.

I've done a fair bit of development on this web app already using the standard Tomcat connection pooling so I was hoping to implement the change to where and how the pools get created with as little disruption to my existing code as possible. So, if I try to use a connection pool other than the one built into Tomcat.... what would be the best way to go about that? The problem I've run into so far is that I'm getting type matching errors with the "DataSoure" class if I use the code that I posted early to create the connection pool.

Can anyone recommend an existing pool implementation that would work with the javax.sql.DataSource interface? (again, if this is a REALLY stupid questions, I'm happy to be educated about what I obviously don't understand... which could take I while because I know there's a LOT I don't understand).

Below is a snippet from one of the Servlets I've already written.... pretty much all my Servlets have this kind of code in them.





8 years ago