If you use the Sun Java System Application Server (SJSAS) with the MySQL database application, you will eventually notice that communication between the two products "goes to sleep" after prolonged periods of inactivity. I've traced this to the following root cause:: Communications link failure due to underlying exception:
java.io.IOException: An established connection was aborted by the software in your host machine
at sun.nio.ch.SocketDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:33)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:104)
at sun.nio.ch.IOUtil.write(IOUtil.java:75)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:334)
at com.sun.enterprise.server.ss.provider.ASOutputStream.write(ASOutputStream.java:138)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2637)
Something goes stale during down time and it can't be revived until you restart the server itself. Restarting the database doesn't help. For servers always being accessed, the issue won't be seen. I'd most likely blame the application server if it were up to me, but I'm not conclusive on that, since it may also mean that a socket connection to MySQL's port failed. Just not sure!
An easy workaround is to implement a thread in the application that keeps exercising the connection if it hasn't been used in a specified period of time. When I did this, my problem disappeared.
From emails of readers wondering how I have my development environment set up, I decided to get this documented somewhere in response. I promise it's the simplest setup ever, and you can get cranking on code and produce applications much sooner than other options I have tried.
For basic Java EE 5 development, you need a Java IDE, an application server, the JDK, and a database application. Here are details of what my environment consists of:
Development System:
2.66GHz Pentium 4 HP Pavilion laptop with Windows XP SP2 and 1.2GB RAM. The emphasis here is a fast processor and gobs of memory. I also recommend about 4GB of hard disk space for the tools and projects.
Integrated Development Environment:
NetBeans 5.5; came bundled with Sun Java System Application Server 9u1 (or you can always associate it later). You should also have JDK 5.0 or later installed.
Remember to update system environment variables such that when you run java -version on the command prompt, it is successful. You can debug applications on the server from the IDE quite simply.
Database Application:
MySQL 5.x. You can set up SJSAS9u1 to use this database for your applications by setting up references to it from the admin console. SJSAS comes with Apache's DerbyDB database, but I prefer MySQL because it is production grade and comes with admin tools.
That's all there is to it. Pretty novice, isn't it? But as any good programmer, you should have alternatives on standby for when your primary environment seems to misbehave. I maintain an installation of the JBoss Application Server 4.0.3, the Eclipse 3.x IDE/platform, MS SQL Server 2000 Developer Edition, and (believe it or not) JBuilder 2005 Foundation. Except for MS SQL, everything I do development on is free (or costs very little).
As you get more advanced with the language and application development, you'll find need for decompilers, profiling tools, unit test and optimization tools, productivity and feature packs (such as the web pack or C/C++ development pack), modeling tools, and other integration tools. You might want to use CVS at some point for source code control and start building your own libraries and templates.
This setup is only suitable for desktop and web applications. More advanced programming will certainly require other tools. For example, if you do reverse engineering, this setup will not help you. Keep an eye out for posts I'll do on reverse engineering. In the first such posts, I'll focus on r.e.'ing DVDs and CDs with so-called protection. That topic will be my graduate school dissertation.
As to my qualifications, I'm simply still a college student. Almost all my programming/computing skills are self-taught (read lots of books, surf the web, code a lot, learn by experience). Very little has come from my formal training. I have a couple of programming certifications in Java and C/C++, database (MS, Oracle), and MCSEs. Lots more valuable experience working in telecommunications (programming cell phones) and storage device development (read FC, SAS and SCSI storage controllers). Nothing special there, except my passion to learn and use what I learn. I also contribute code regularly to open-source projects such as those on SourceForge.com. Finally, I don't like Microsoft stuff - if I knew Linux well enough, I'd move all my development to it (some day). Although I can program with C# and other .Net technologies, I'm not even listing them here ...
Hope this answers the emails. Thanks for those, by the way.
Follows [Part I] ... The Entertain(r) application stores (or persists) data in a database. For now, the only data we need to store is about movies, including the title, the MPAA rating, the DVD number in my library, the date added to the library, the genre, and a short synopsis. The movie data will be represented in a Movie class, which will be annotated to become the entity. Tools: NetBeans 5.5, Sun Java System Application Server 9, MySQL 5. Create the Persistence Unit Persistence units describe the datasource to the application. - Right-click the ejb module > New > File/Folder | Persistence > Persistence Unit > [Next]. Datasource=jdbc/entertain. [Finish]. A persistence.xml descriptor file is created. Create the Entity An entity is a representation of an object in the datasource. Since we plan to persist movies, it's only appropriate that we call the movie entity Movie. Entities are POJOs that are annotated to indicate that they should be persisted into the database. - Right-click the ejb module > New > File/Folder | Persistence > Entity Class > [Next]. Class Name = Movies; Package = movie.ejb. [Finish]. An entity class is created and opened in the IDE. It already has the @Entity annotation, along with @Id/@GeneratedValue (auto-generated primary key), is serializable, overrides equals(), toString(), and hashCode() methods - all requirements for an entity class. - Add the entity's fields: String title; String genre; String rating; String synopsis; Date dateAdded; int dvdNumber; - Generate bean-style getters and setters for the fields: right-click in the source file > Refactor > Encapsulate Fields ... - Add the @Temporal(value = TemporalType.DATE) annotation for the dateAdded field. This is so that the persistence provide can translate dates correctly to the TIMESTAMP data type used in most databases. Create the Session Bean The session bean provides a facade that can be used to perform CRUD operations. - Right-click the ejb module > New > File/Folder | Persistence > Session Beans for Entity Classes > [Next]. From the available list, select 'Movie (movies.ejb)' and click [Add] then [Next], then [Finish]. A stateless session bean is created and includes methods to create, edit, destroy, find, and find all movies. ** Fix:: Change the destroy() method to ensure that we operate on a managed object: public void destroy(Movie movie) { Movie managed = em.merge(movie); em.remove(managed); } Create the Servlet We'll use a servlet to test the persistence layer (model, par MVC), which includes the data source, persistence unit, entity class, and session bean - so far all defined in the context of the EJB module. The servlet shall exist in the web module, however. - Right-click the war module > New > File/Folder | Web > Servlet > [Next]. Class Name = MovieServlet; Package = movie.servlet; [Finish]. A servlet class is created an opened in the IDE. - Inject the session bean resource: right-click in the source > Enterprise Resources > Call Enterprise Bean. Select MovieFacade, click [OK]. This create an @EJB annotated reference to the session bean. - Enter some useful code in the processRequest() method (to which all POST and GET requests are redirected). In this application, we will enter decision logic for the CRUD operations we need to test. To add, edit, remove, and find a movie, call the movieFacade methods. [Full servlet code] - Remember to change the servlet's URL pattern to something simpler, like /Movie - The application should be deployable now, accessible at something like http://localhost:8080/Entertain-war/.
Results:
The servlet is nowhere near perfect (for example, it does not check data lengths before posting to database fields - defaults are 255 characters), and has a lot of hard-coded lines. But it serves the simple purpose of testing the persistence layer, now composed of the data source, persistence unit, entity class, and session bean. If you are into MVC development, that layer is the model part, and the servlet serves as both the controller and view.
The Entertain project will be used to create the Entertain(r) web application, which contains MyMovies. As introduced earlier, MyMovies is a simple application used to manage/catalog my growing library of movies on DVD. It is a learning project meant as a simple how-to for those just starting to program with Java EE 5 technologies (especially JDK 1.6 and EJB 3). It focuses on using the NetBeans 5.5 IDE, Sun Java System Application Server 9, and MySQL 5.Setup the database in MySQL- Open MySQL Administrator and click on Catalogs. Right-click the bottom left pane and choose "Create New Schema" from the popup menu. Name the schema "entertain". This will be the database to use for the Entertain application, which contains MyMovies.Add a JDBC resource in Sun Java SAS- Copy the MySQL Connector/J JDBC driver JAR (mysql-connector-java-5.0.4-bin.jar) into ${SJSAS}/domain/domain1/lib/ext. This ensures that driver classes are included in the application server's classpath for the domain.- Start the application server, if it not yet started, and log in using the Admin Console.- Select Resources > JDBC > Connection Pools. Click [New...] to create a new pool. Name=EntertainDB; Resource Type=javax.sql.ConnectionPoolDataSource; Database Vendor=mysql. Click [Next]. Datasource Classname=com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource. Down under Properties, databaseName=entertain; serverName=localhost; port=3306; user=root; password=[whatever you assigned to the DB for root]. Click [Finish].- Back in the list of connection pools, select the EntertainDB pool link and click [Ping]. If the connection was successfully set up, you will see a "Ping Successful" message.- Now set up the JNDI resource that will be referenced in the application: select Resources > JDBC > JDBC Resources and click [New...]. JNDI Name=jdbc/entertain; Pool Name=EntertainDB. Click [OK] to create the resource.Create a new project in NetBeans 5.5- File | New Project ... > Enterprise > Enterprise Application > [Next]. Name="Entertain", other defaults ok [Finish].The enterprise project created will be used to create the EJBs and webapps that we need to implement MyMovies. Create a user-friendly URLJust so we don't forget to deal with it later, we need to set the web module URI for the application to /entertain so that from a web browser, it can be accessed as "http://localhost:8080/entertain" (assumes SJSAS listens on its default HTTP port 8080).- In Netbeans, right-click the Entertain-war webapp node and select Properties from the menu. Select the Run node and set Context Path=/entertain, then click [OK].Clean up the welcome pageA index.jsp page already exists in the web module (Entertain-war > Web Pages) that will be opened when the above URL is sent from a browser. You can edit it to better suit requirements as the home page of the application.<%@page contentType="text/html"%><%@page pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Entertain(r)</title> </head> <body> <h1>Welcome to Entertain</h1> Entertain is a web-based application used to manage personal entertainment artifacts. </body></html>Deploy and TestThe application cannot be deployed at this time because Java EE 5 enterprise application require at least a Session bean or Message-Driven bean.
In an attempt to make life easy for myself, I've decided to develop a small web application that I'll use to manage my growing list of movies. Enter MyMovies, which will be allow adding movie information, updating, deleting, searching, and listing. In the initial release, movie information will include the title, the DVD media number where the actual movie is burned, its MPAA rating, genre, date it was added to my labrary, and a brief synopsis.Beyond these simple details, the application is meant to demonstrate using the NetBeans 5.5 IDE to develop applications for the Sun Java System Application Server 9.0 update 1 and the MySQL 5.0 database application. All the steps will be posted here for those just getting started with EJB 3.0 and other exciting Java EE 5 technologies that I may choose to play with in this application. This is my way of brushing up and learning some of the new features in Java.At the conclusion of this track, I should have a fully functional web application that:(1) allows me to add movie information(2) allows me to search and list the movies(3) allows me to update or delete movie information(4) uses a pure Java EE 5 solution based on EJB 3.0.I'm turning out to be the free movie rental service for people on my block, so I figured this site might as well help me keep track of who's borrowed my movies. I could also extend it to a multiuser environment, where friends can recommend movies to each other freely or keep track of what they've watched or want to watch. Then what would be cool is to port it as a plug-in for other applications and link it to big movie databases out there somehow. I'd have learned a great deal by that time, I suppose.
I've been using NetBeans 5.5 a lot lately, and I must say I like it and have decided to stick with it. NetBeans comes bundled with the Sun Java System Application Server, another product I have come to appreciate lately. These two are are the latest additions to my arsenal of development tools, alongside Eclipse and JBoss AS. I've all but dumped JBuilder (they are moving to an Eclipse base anyway).The SJAS comes bundled with JavaDB, and open-source Java database. It's great I suppose, but without a management console, it hasn't been easy to use. So I'm switching to MySQL. The following steps are for setting up MySQL in SJSAS, and developing applications in NetBeans that'll use it. - Ensure that the NetBeans 5.5 and Sun Java System AS 9u1 setup works, as in, you've done deployments to the server from the IDE. In NetBeans, you should be able to monitor and control the server from the Runtime view. You should also be able to debug easily on the server from NetBeans.
- Download the MySQL database driver for JDBC (Connector/J). Extract the JAR file to domains\domain1\lib\ext. In that location, it'll be included in the application server's classpath.
- Download and install MySql 5.0 and install it on your machine. I set mine to run as a service (though not at start up). Most defaults worked fine.
- Download and install MySQL Tools (which includes an administrative console). I used this to create a schema for development use (in Microsoft's world, these would be "databases").
- Open the web-based SJSAS console. Go to Resources -> JDBC -> Connection Pools -> New ... Provide a name, use the connection pool datasource for resource type and select mysql for vendor. Datasource class name = com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource. Add properties - user, password, server, schema (created in step #4), and port.
- Go to Resources -> JDBC -> JDBC Resources -> New ... to create a JNDI resource. Its name should start with jdbc/someName. Select the connection pool created in #5. If it starts without error, #2 is confirmed and you are on your way.