Wednesday, November 01, 2006

Setting up a connection pool with Tomcat

I made so many attempts to make database connection pooling work with Netbeans and Tomcat, and little did I know that the solution is just in the Netbeans help documentation. Look up the topic "Setting up a Connection Pool on the Tomcat Web Server". In any case, here are the steps:

Assuming you already have a web application project open,

1. Go to the Tomcat administration page.
  • In Netbeans, go to the Runtime Window.
  • Expand Servers
  • Right-click on Apache Tomcat and Select Start
  • Expand Apache Tomcat
  • Expand Web Applications
  • Right-click on /admin
  • Select "Open in browser"
  • Enter username and password.(SEE NOTE BELOW!)

NOTE: Ironically, this is the most difficult part in setting up connection pooling. I had to spend a lot of time trying to find out what the default username and password is for the Tomcat Admin. So follow the instructions here carefully!
  • right click on the Tomcat server instance in the Runtime window.
  • select Properties
  • note the value in the Catalina Base field. Mine is C:\Documents and Settings\Gavin\.netbeans\5.5\apache-tomcat-5.5.17_base.
  • Go to this directory, and you will see a directory in it named "conf".
  • Under the conf directory, open tomcat-users.xml.
  • Look for the user whose role is "admin". That's the user you are looking for. Mine has the username "ide".

2. Add a JDBC Database Resource.
  • Once logged in, click Data Sources
  • Choose Create New Data Source Actions drop down in the right.
  • Type values such as the following to define your data source. My settings look like this. Change yours accordingly.
    • JNDI Name: jdbc/grocerificDB
    • Data Source URL: jdbc:mysql://localhost:3306/grocerific
    • JDBC Driver Class: com.mysql.jdbc.Driver
    • User name: root
    • Password: secret
3. Make sure the JDBC driver is placed in the common/lib directory of Tomcat's base directory.
NOTE:
The driver has to be placed there and not under your application's WEB-INF/lib directory. I really don't know why. Netbeans' bundled Tomcat server is in
<Netbeans folder>\enterprise3\apache-tomcat-5.5.17\common\lib.

4. In your application, edit WEB-INF/web.xml. Add the following lines:

<resource-ref>
<description>Grocerific DataSource</description>
<res-ref-name>jdbc/grocerificDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

5. Replace jdbc/grocerificDB with the name you registered in Tomcat.

6. In your web app's META-INF directory, open context.xml, and it should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/DataSourceWeb">
<ResourceLink global="jdbc/grocerificDB" name="jdbc/grocerificDB" type="javax.sql.DataSource"/>
</Context>

7. Replace jdbc/grocerificDB with the name you registered in Tomcat, and replace /DataSourceWeb with your application's name.

8. To reference your DataSource from a servlet,

Context ctx = new InitialContext();
Context envCtx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/grocerificDB");
Connection conn = ds.getConnection();

That's it! let me know if you have any questions. I just discovered a super easy way to do all of this using Netbeans wizards. I'll post it when I have the time.

It's amazing how Netbeans has progressed. I still wonder why so many people still use Eclipse when Netbeans seems to be so much better.

Visit Active Learning if you want to attend my training courses on Java and J2EE.

Friday, October 13, 2006

Macbook design flaws

I just recently bought a white Macbook. Coming from the Windows world, I was looking forward to the much hyped Mac experience. I'm a bit disappointed.

First - the white Macbooks easily get dirty. As obsessive compulsive I am using my Macbook, I can't help but notice darkened areas in the wrist pad after occasional use only. I find myself buying a silicone skin for the keyboard and wrist pad worth P1800 just to avoid this problem. I say this is a rip off when they actually sell these for just $5 each in China.

Second - I have been very careful handling the Macbook, but it still gets some light scratches on the outer surface. The material is just very prone to scratches, and I wonder why Apple insists on form over functionality. Go to a Mac store. They even sell scratch creams just for this. They're actually making money out of the design flaw. Great huh? My friend has the same problem, so I can't say that this is an isolated incident. I'm sure that Macbook users here experience the same thing.

Third - In a world where 2 mouse buttons are already considered passe', I wonder why Apple insists on a one button trackpad. Hey, the second mouse button actually works when you connect a mouse. So why didn't they just add a second mouse button to the Macbook? Tradition? Maybe I'm just new to the keyboard, mice, and the interface, but I looked at the keyboard shortcuts, and noticed that I had to hold down one more button to do this and that. It's kind of annoying.

Fourth - There's just something wrong with the edges on the wrist area of the Macbook. They're so sharp, I find myself rubbing my wrists from time to time.

Fifth - Who the hell uses Mini-DVI? I do a lot of presentations, and to use my Macbook, I have to buy a $20 mini-DVI to VGA adapter to connect the Macbook to a projector. Oh, they sell it P1,800 locally.

Sixth - You have to use a coin to take out the battery? I don't use the battery when my Macbook is plugged in, so I take it out often. I find it annoying that I have to look for a coin whenever I want to take out the battery. Why not just a simple battery release?

On the other hand, the MacOS looks stunning, although I haven't gotten used to the interface and keyboard shortcuts just yet. Spotlight's speed is amazing. OS is rock solid. Haven't had a restart nor a hang since I bought it, well except when I had to update to MacOS 10.4.8. And the boot up time is just unbelievable.

Sorry guys, I just don't know why there's such a hype over macs. After my first month in the Mac world, all I can say is, Mac is nice, but it's not that great. I do hope it's just the transition that's causing all the disappointment.