Tuesday, November 30, 2010

Databases and Threads

The usual disclaimer applies to this post: millions of people probably already know this, and will be thinking 'What? And you call yourself a programmer??'

What could possible be wrong with the following function:-

public int RunIdentityInsert(String sql) throws SQLException {
Statement stmt = (Statement) conn.createStatement();
stmt.execute(sql);
return getScalarAsInt("SELECT @@IDENTITY AS NewID");
}

How about the bane of my programming life, threads? I've got loads of programs that have been running on servers for years using the above code, and mostly without a problem. Until yesterday.

My function added a record to a table containing about 6,000 rows, but the insert returned an id of about 250,000. It was only after looking at all the other possible reasons that this probably cause hit me: what if another thread ran another insert statement at pretty much exactly the same time? That would surely cause the same symptoms.

To be honest, I don't actually know if that was the cause, but it seems to me that the code should look like this:-


public int RunIdentityInsert(String sql) throws SQLException {
synchronized (conn) { // Need in case another thread also does an insert!
Statement stmt = (Statement) conn.createStatement();
stmt.execute(sql);
return getScalarAsInt("SELECT @@IDENTITY AS NewID");
}
}

Wednesday, November 03, 2010

Why I like Java:

Because when I wrote a slow crap program, everyone blames the language and not me!

Thursday, July 22, 2010

The P*ssed Off Blog - Munin

I now update this blog rarely, and the only time I will probably write here is when I'm COMPLETELY P*SSED OFF with some software or other. And today it's Munin, a program to profile server, like network and CPU usage etc...

It seems like a great program, and worked great on my local VM which is running an old-ish version of Ubuntu (Hardy Heron, 8.04) and version 1.2.5 of Munin in Synaptic. I had it up and running in minutes thanks to this article.

Of course, you might have an idea what's coming; it's what happens with most great software programs: the programmers don't know when to STOP F*CKING AROUND WITH IT and start adding/removing features and generally making something that "just worked" into a friggin' nightmare to use.

After I installed Munin (1.4.4) on the latest version of Ubuntu (Lynx, 10), I ran into problems trying to run it. Unhelpfully, when trying to run Munin as root, it comes up with the message "this program will break if you run it as 'root'...". Jesus, I'm not some n00b. Anybody who wants to run something like Munin is not a casual computer user, and knows the problems associated with running everything as root. But no, the programmers of Munin obviously think they know better and want to protect us from ourselves (and getting any work done).

The older version of Munin has a switch (--force-root) to over-ride this. But that was too helpful so THEY'VE TAKEN IT OUT. Thanks a lot. The message does come with some help: "run it as user 'munin'". Erm, ANY CLUE AS TO WHAT THE PASSWORD MIGHT BE? No? Thanks for nothing. I've tried "munin" and blank, but no joy and Google doesn't seem to be able to help me either. I'm not even sure that would work anyway, as the file referred to in the "permission denied" error I get is owned by root. So do I have to chmod loads of seemingly random files to allow user 'munin' to edit them? Surely that's no way to have a secure system?

So to sum up, I got the older version working in about 5 minutes, but so far I've spent a couple of hours on the new version and got nowhere. Thanks, munin programmers, for taking Munin backwards. I won't be using Munin again, but that's more because I COULDN'T IF I WANTED TO than due to any principles and animosity.

Thursday, April 01, 2010

Eclipse and Subversion [SOLVED]

This is mainly a note for future reference for myself, since I always seem to have an ABSOLUTE F*CKING NIGHTMARE trying to get Subclipse working on any new install of Eclipse. Thankfully it seems to have got better in the last few months now that Eclipse automatically finds dependencies, and you don't have to realise that Subclipse requires Buckminster, Buckminster requires ECF, and ECF requires Equinox until YOUR HEAD EXPLODES.

Anyway, now all it seems you have to do is select the Tigris update site as per here and select all the Subclipse components.

Then finally, for the magic step that makes it all work that took me about 3 hours searching the internet for, you have to right-click on your project and select Team -> Share.

(Why it couldn't realise that a project is already in subversion by seeing if there were any .svn directories is something I can't answer. There may be good reasons for this).

Friday, January 08, 2010

Developing a Community

One thing I always aim to achieve with my games is developing a community around them. This has pretty much eluded me (at least to my standards) until now, as there there is a small community playing Stellar Forces.

However, I'm confused. I've just looked at the main page of a new game called Mule, which is an online remake of an old C64 game (which I've never played, BTW as I'm an old Speccy user and it would be against my religion to do so). As far as I can tell, it's only been going since December last year, and it already has over 8,000 registered users! There other stats are impressive to - 1,600 forum posts, 2,800 games played etc...

So my question is "What are they doing to get that many users?" It's obviously a far more polished affair than my own effort - is that it?

Monday, January 04, 2010

Use of Overriding Methods Considered Harmful

I've come to the conclusion that overriding methods is a bad idea.

You know the situation; you've subclassed another class, which has a method in it that's not quite perfect, so in your subclass you write another method (giving it the same parameters) that does something slightly different.

Cue several months later, and you innocently edit the method in the parent class, changing the method parameters. Suddenly bugs appear left right and centre. "But how?" you ask yourself.

Well, it's because the method in the subclass is now never called since the method signature is different to the parents signature. But how were you supposed to know? Exactly!!