Tuesday, November 04, 2008

Deleting SVN Directories from a Project

Sometimes you just need to get rid of those pesky subversion folders because you can’t export or something so here’s the quick and easy way to do it in windows:

  • Be sure you can see hidden files and folders (Advanced folder settings)
  • Open Windows Explorer in the root folder of your project
  • Explorer
  • In the search bar (or Ctrl+F for older versions of windows), type “.svn”
  • Sort by name
  • Delete all the ones that are folders (not the ones that are files)

Done.

Tuesday, October 07, 2008

Callable and the Case of the Disappearing Exceptions

If you have a class that implements Callable, but you don't plan on using the Future that is returned after you call Executor.submit(), you may lose any exceptions that are thrown. They literally disappear into the void. No log whatsoever.

So it is good practice to treat it a bit like you would with a Runnable and wrap the code in your call method with a try/catch to print the exception or log it somewhere.

Thursday, October 02, 2008

How To Give a User Root Access on Linux

NOTE: I take no responsibility for using the following and do not recommend using it.

But for those that want it:

usermod -G root username


This changes the user with ‘username’ to the root group and thus gives root access.



And remember, as Uncle Ben says, “with great power comes great responsibility”.

Wednesday, October 01, 2008

Using JAXB Without Annotations

The latest JAXB included with JDK 1.6 is really a great addition to the JDK making it easy to send objects around in XML format without requiring any third party libraries. All you have to do is put an @XmlRootElement annotation on a class and then call:

marshaller.marshal(Object, OutputStream);

Example class using @XmlRootElement:

@XmlRootElement
class MyClass {
  String name;
  String content;
}


To read the object back in, you simply call:



MyClass object = unmarshaller.unmarshal(InputStream);



But what if you are building a library that doesn’t know what the class is and doesn’t have the @XmlRootElement annotation?  Well it turns out that it’s almost just as easy without it you just need to let JAXB know what class you expect.



First get the JAXBContext like this:



JAXBContext context = JAXBContext.newInstance(object.getClass());


To marshal:



marshaller.marshal(new JAXBElement(new QName(object.getClass().getSimpleName()), object.getClass(), object), out);


To unmarshal:



JAXBElement o = unmarshaller.unmarshal(new StreamSource(inputStream), expectedType);
MyClass object = o.getValue();


That’s it.

Sunday, May 25, 2008

Warming Up the Hibernate Second Level Cache

I recently ran into a problem where Hibernate took forever to load up a result set because it was executing two additional queries for every object in the list to get it's @ManyToOne references. With thousands of objects returned, this meant thousands of individual sql select queries! I am using the JPA EntityManager throughout these examples.

eg:

public class MyEntity{
....
@ManyToOne
public MyEntity2 getMyEntity2(){
...
}

@ManyToOne
public MyEntity3 getMyEntity3(){
...
}

}

I had to use these many-to-one references so fiddling with lazy loading wouldn't help my situation. I was already caching the result collection after the first load, but it still was taking minutes to load up the first time and there was less than a thousand results, what gives? But 1000 * 2 extra sql select queries is 2000 individual queries which is just not good. Even the Hibernate second level cache didn't help because it still needs the first load.

There must be some way to make this startup faster right??

So I starting thinking, "how can I get these things to load up in the fewest number of sql queries?". Then it struck me, I'll warm up the cache by querying for all of the ManyToOne objects on startup, for instance:

em.createQuery("select me2 from MyEntity2").getResultList();

and

em.createQuery("select me2 from MyEntity3").getResultList();

Now when I run the original query for MyEntity, the result is almost immediate. I turned ~2000 database hits into 3!

More Reading:

Tuesday, May 20, 2008

Automated, Real-time Hard Drive Backup

As I've said before, I'm a big advocate of backup systems because I've gone through the hell of hard drive failures. A 2007 study by The Computer Science Department of Carnegie Mellon University states that:

We find that in the field, annual disk replacement rates typically exceed 1%, with 2-4% common and up to 13% observed on some systems.
With statistics like that, you better believe it will happen to you some day.

I currently use Mozy for remote backup in case I throw my computer out the window because Vista has me so frustrated. Remote backups are great and a must have in my eyes (they also give you that warm fuzzy feeling), but they are not perfect. For instance, it could take weeks to actually recover your 50 gigs of data from a remote service. You will eventually get it all, but it won't be a quick recovery.

Want zero down time? Enter RAID. Accordance Systems has a really cool backup device called the ARAID that automatically and transparently backs up your hard drive by mirroring it. Now mirroring hard drives isn't anything new, but the cool part about the ARAID device is that it acts like a normal hard drive on your computer and you can simply add it to your box without having to do anything crazy like reformatting your hard drive or installing raid controllers to get it running. And if a hard drive fails, you won't be waiting weeks to recover, in fact you won't even have to stop working because the mirrored drive takes over.

Here's the skinny on how it works:
  1. You shutdown your computer (or course)
  2. Replace your master hard drive with the ARAID unit
  3. Put your master hard drive in the top bay of the ARAID
  4. Now you start up your computer and it runs like normal as if the ARAID weren't there
  5. Now you insert a second drive into the bottom tray (yes, you can do this while the drive is
  6. The ARAID will now synchronize the master drive to the second drive on the fly
  7. Now you have a fully functioning mirrored RAID 1 array where your data is always backed up to the second drive so if one drive fails, the other takes over with no downtime.
This is pretty cool stuff, especially because they made it so easy to setup. I would definitely recommend using something like this along with a remote backup solution just in case your office burns to the ground.

Thursday, March 06, 2008

How To Set Up Dynamic DNS for your Amazon EC2 Instance

Here's a step by step to setting up dynamic dns for you EC2 instance. This is required because EC2 does not have static IPs (yet).

  1. First of all, you need a dynamic dns account. You can get one at www.dyndns.com or my personal favorite, www.zoneedit.com.
  2. After you sign up, be sure to add your domains to their system.
  3. Download dyndns client 
    1. run: wget http://cdn.dyndns.com/ddclient.tar.gz
    2. The download includes Installation Instructions in the README, but the step by step is below
  4. tar -zxvf ddclient.tar.gz
  5. cd ddclient-X.Y
  6. cp ddclient /usr/sbin/
  7. mkdir /etc/ddclient
  8. nano -w /etc/ddclient/ddclient.conf
    1. Then fill in config file:

FOR ZONEEDIT

# Basic configuration file for ddclient
#
# /etc/ddclient.conf
daemon=600
pid=/var/run/ddclient.pid
use=web, web=checkip.dyndns.org/, web-skip='IP Address'
login=[USERNAME
password=[PASSWORD]
protocol=zoneedit1
server=www.zoneedit.com
wildcard=YES
[YOUR DOMAIN(S), COMMA DELIMITED LIST]

FOR DYNDNS

# Basic configuration file for ddclient
#
# /etc/ddclient.conf
daemon=600
pid=/var/run/ddclient.pid
use=web, web=checkip.dyndns.com/, web-skip='IP Address'
login=[USERNAME
password=[PASSWORD]protocol=dyndns2
server=members.dyndns.org
wildcard=YES
[YOUR DOMAIN(S), COMMA DELIMITED LIST]
custom=yes, example.com

More info on configuration

Continuing on:

  1. cp sample-etc_rc.d_init.d_ddclient /etc/rc.d/init.d/ddclient
  2. /sbin/chkconfig --add ddclient
    1. This makes it startup automatically next time you reboot
  3. /etc/rc.d/init.d/ddclient start
    1. This is to start it the first time by hand
    2. DON'T RUN THIS until your server is fully setup if you are moving servers because people will start hitting this server after you run this

That's it, not too difficult and gives you a real domain name to point at your EC2 instance.