I've seen projects where the application code base contains database connection information - passwords and all, or, WAR files are rebuilt for each environment with properties substituted appropriately for the target environment.

Rather than putting database information in the application code, it makes more sense to use a JDNI data source provided by the container. This way the exact same application code is deployed to all environments, and we don't encounter any exposed passwords - since the owner of the container (in production environments the infrastructure/support team) define the data source themselves.

Database access via JNDI data source in Tomcat

In your application server you want to define a data source which points to the database server and schema of your choice. Here, I'm using the H2 database, and assuming this is the DEV Tomcat server, I point it to the DEV schema - define this resource in the Tomcat context.xml:

 <Resource name="jdbc/app1db" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="sa" password="" driverClassName="org.h2.Driver"
               url="jdbc:h2:tcp://devDbServer/~/dev"/>
In your TST, UAT, PRD servers you define similar data sources, pointing to the appropriate schemas, all with the same name.

Your application server MUST have the database driver on its classpath, so you will want to add the appropriate driver jar to TOMCAT_HOME/lib.

Other containers such as GlassFish, WebLogic, WebSphere and JBoss will have equivalent ways of achieving this same configuration.

Now, in your application all you need to do is define the JNDI data source, which never changes:

    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/app1db"/>
    </bean>
With the database connection information encapsulated within the container, you don't need these details in the source code, you don't expose passwords to anyone and you don't need to repackage your application for each environment - deployment is simple. 

Another similar use-case is for the mail server. You may also want to expose a String which could point to a properties file or server address - this can then be referenced in your spring application context.


Below are xml snippets of a Tomcat configuration which exposes some resources and a Spring application context file that uses those resources.




It's sometimes nice to be able to update a development Tomcat 7 server from Maven - this makes it simple to hook automatic deployment into a CI server or just update the dev server as a developer.

First, the Tomcat manager application needs to be installed (check Tomcat's webapps directory for the manager application) and configured with the appropriate user credentials:

Now, I needed to define the server admin credentials in my maven settings (~/.m2/settings.xml):

Then, I updated the POM to configure the maven tomcat plugin:


Now, using 'mvn tomcat7:redeploy' lets me update the dev server.

Note however, on Windows you may have some problems with undeploying the application - after an undeploy command, some jars may be left over in the webapps/appname directory. When you try to redeploy your app you'll see an error containing "cannot invoke tomcat manager fail - unable to delete...".

To work around this, you can change the TOMCAT_HOME/conf/context.xml to include the 'antiJARLocking' attribute like so:
<Context antiJARLocking="true">
The documentation points out though that this will impact start up times of applications.

In my case, I noticed problems when doing a redeploy to Tomcat - most likely unrelated to Maven and/or the maven tomcat plugin and more to do with PermGen (I saw perm gen OutOfMemory: PermGen space errors in the tomcat7-stderr logs, and the Tomcat process was consuming 100% cpu). Adding the following switches to the Tomcat JVM settings seems to have fixed it for now:
-XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled -XX:PermSize=64m -XX:MaxPermSize=128m -Xmx992M
 (In this instance, I'm running Tomcat 7 as a Windows service on JDK7 with a 50MB WAR file).
photo credit: markchadwickart
via photopin cc
I just encountered this exception:
Exception in thread "main" java.lang.AbstractMethodError: com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.setCharacterStream(ILjava/io/Reader;J)V at org.hibernate.type.descriptor.sql.ClobTypeDescriptor$4$1.doBind(ClobTypeDescriptor.java:114) at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:92)
It turns out that upgrading c3p0 does the trick (from 0.9.1.2 to 0.9.2.1):

        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.2.1</version>
        </dependency>

Print to Google Drive

I just noticed 'Save to Google Drive' in my 'Print' options in Chrome! This is awesome - when you want to save something for reference, and you don't want to go through thousands of bookmarks, this is exactly what I wanted!

Media Problems

Here is a great blog post about trying to find a decent movie streaming service. Now I have FetchTV (this lets me buy movies to stream, much like the Google Play option) I'm considering turning my MythTV media center off, but I still need an option for playing music/displaying photos/etc. There are lots of options, from AppleTV to Western Digital et al media players. Props to Google for actually selling us here in Australia movies and music - unlike Amazon (we still can't buy movies and music from Amazon in 2013).

Andrew Chen quits RSS - WTF!

Andrew Chen just announced that he's quitting RSS in favour of email notifications. Look at the comments though, and there isn't a single positive response (and they are all quite sensible reactions). Presumably this was provoked by Google Reader being retired, but the unanswered question for Andrew is why does Reader == RSS ? I've switch to Feedly (which has a seamless process to migrate your reader account) and I'm liking it so far. I won't even notice Reader go offline.

Links



I came across this problem with an older project - when I switched from jdk6 to jdk7, when the application started I encountered:
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException:  error annotation type patterns are only supported at Java 5 compliance level or above
I didn't want a purely maven solution because during development I run the app in Tomcat via the IDE (Intellij IDEA).

In the end I just upgraded my aspectjweaver dependency to 1.7.2 (it was 1.5.4)

<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.7.2</version>
</dependency>
Everything seems okay now.

photo credit: @Doug88888
via photopin cc
Installing Windows Vista

For the first time since I bought it, my old Dell Inspiron 1525 has Windows Vista back on it. It's been running Ubuntu Linux since I got it, so why does it now have Windows again? My children are using computers at school now and they are using Windows - mainly Office. So rather than confuse them with different operating systems it seems more constructive to just give them some consistency and let them have their own computer set up like it is at school. I'm going to have to buy Microsoft Office for them - which after seeing how Libre Office handles clip art, I'm quite happy to do! (MS Office has a nice search and browse for clipart which the kids love and can use well. Libre Office didn't seem to have any search and you were left to click into each folder one at a time to see the clipart - not particularly useful).

Downloading statements

I've just noticed PayPal have an interesting feature - you can download your transaction history, by date range, OR just the transactions since your LAST DOWNLOAD. Awesome - many sites are missing this feature. Some of the online banking sites don't even remember ANYTHING - date range, file type (csv etc) - from one export to another - you have to enter everything for every account. Just amazing.


Nexus 7 Android Tablet

I finally have an Android tablet - I've been thinking about getting a Kindle for a long time as a way to catch up on reading during my daily commute. The paperwhite looked good but I couldn't order it from Amazon because they aren't shipping it to Australia and Dick Smith only just started selling it at $169. So when ShoppingExpress had the Nexus 7 16GB for $199 plus postage, I thought that'd be good. A full tablet instead of an eReader, and a little bit heavier, but I think its a good compromise and I can use it for so much more.


New screen protector for my phone

When I got my HTC Incredible S (on an Optus plan) they put a screen protector on it before giving it to  me. So I never saw the screen without it. The protector had a kind of grainy-ness to it but the screen looked okay. Recently the protector started looking pretty bad and it was time for a new one - and WOW, what a difference. This protector is different, not grainy, totally clear but I assuming its not too good for reflection! It was also very easy to put on since it is a stiffer plastic and doesn't bubble. Right now my screen is looking totally awesome and like a brand new one!

Links



Google Reader and Listen

I've been using Google Listen for listening to podcasts since I got my Android phone. It worked well, I would just subscribe to podcasts via Google Reader and drop it in the 'Listen Subscriptions' folder. But now with Google announcing the end of Reader, and Listen has long been abandoned, I'm looking for a suitable alternative. I really liked how I simply had to subscribe to a feed online - if I have to subscribe to things via an app, I'm stuck if I ever want to switch apps. Maybe I'll try Pocket Casts.

LinkedIn Groups

I've noticed that when contributing to LinkedIn groups, its not immediately obvious to others from your profile. Recent comments might show up in your activity stream but they'll soon disappear. If you view your profile, you'll see a Groups section that shows the groups you have joined. Click on one of those and you'll be taken to the Group page and at the top there will an area where you can start a discussion - on the left will be your picture and under that is a tiny link to 'Your activity'. Click on this and you'll be able to navigate your history within this group. You can link to this page using the group id and your   member id. Mine link to Agile and Lean Software Development looks like this:

If you are viewing a discussion, you can click on the picture of a commenter to see their activity. 

I'd prefer it if the groups section of my profile had a link to my activity, so it was easy to see. At the moment no prospective employer would ever find it.

Links