Thursday March 30, 2006 Using TopLink persistence RI in Tomcat New EJB 3.0 persistence can be used in web module as well but all samples show this new feature in JavaEE5 projects. I assume that many users want to use this cool feature in web projects that have J2EE 1.4 version. I would like to show in this post how to use persistence in web project that is deployed in Tomcat. Of course, on the Tomcat we can't use container managed entity manager. Therefore, the application managed EM is used in this sample. You can use JTA entity managers or resource-local entity manager. Since the Tomcat doesn't support JTA we can use only resource-local entity manager. Let's create new web project with J2EE 1.4 specification and target server Tomcat in NetBeans 5.5. Don't forget that project should have defined 1.5 jdk as platform and we should change source level to 1.5. Of course, the Tomcat server must run on jdk 1.5 as well. Add JavaEE5 library and JDBC driver on project's classpath.Then, add persistence unit and choose application managed entity manager and generate entity classes from database or create them from scratch. In this post is presented using entity manager in servlet but similar approch might be used in JSF managed bean or in class too. The Entity manager might be used as follows:
EntityManagerFactory emF = Persistence.createEntityManagerFactory("Advertisement");
EntityManager em = emF.createEntityManager();
try{
em.getTransaction().begin();
List advertisements =
em.createQuery("SELECT a FROM Advertisement a").getResultList();
for (Advertisement elem : advertisements) {
out.println(elem.getDescription() + ", " + elem.getId() + "
");
}
em.getTransaction().commit();
}catch(Exception ex) {
em.getTransaction().rollback();
}finally {
em.close();
}
Build and run project. This works on Tomcat but I encountered security issues on Sun Application server 8.2. The workaround is that you should grant permissions for web module in server.policy. Add following lines in this file:
permission java.lang.RuntimePermission "createClassLoader";
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
Now, you can use Toplink persistence in existing web projects instead of Hibernate :-).
Posted by pblaha
( Mar 30 2006, 03:28:46 PM CEST )
Permalink
Comments [6]
Using database in Enterprise application client module. In my last post the new Netbeans project type was presented. Today, I would like to use this project and show how a database connection can be developed in Enterprise application client very simple. Let's start to create application that lists customers that are stored in database.

@Resource(name="jdbc/__default")
private static DataSource ds;
private void readData() {
Connection conn = null;
try{
conn = ds.getConnection();
PreparedStatement pStmt =
conn.prepareStatement("SELECT CUSTOMER_ID, NAME, EMAIL FROM CUSTOMER");
ResultSet rs = pStmt.executeQuery();
while(rs.next()){
custs.add(new Customer(rs.getInt(1), rs.getString(2), rs.getString(3)));
}
}catch(SQLException ex) {
throw new RuntimeException(ex);
}finally {
if(conn != null) {
try {
conn.close();
} catch(SQLException ex) { }
}
}
}
public DisplayCustomerNames() {
initComponents();
cstTableModel = new CustomerTableModel(ds);
jTable1.setModel(cstTableModel);
}

Enterprise application client project in NetBeans My colleague Lukas Jungmann added the support for Enterprise application client in NetBeans 5.5. This
feature is avalaible in the latest Netbeans 5.5 Q-build. He created this new project's type in his spare time. Thanks Lukas.
The project allows to create application client in EAR or as standalone module, user can run, debug the client inside the
IDE.

How to enable logging in file for Java Web Start applications I'm working on application client that is launched via Web Start today. The client application was
downloaded but there was some issue with execution. Therefore, I enabled Java Console in browser. The console
is nice but it's closed suddenly when application can't start and execution is finished. This feature doesn't help me to find out the issue.
Then, I remembered for configuration of Logger for JRE. It's very simple configured via logging.properties file
that is located in lib directory under your JRE home. By deafult, only ConsoleHandler is used and you can very simple
add FileHandler. Add java.util.logging.FileHandler in handlers property. Then you can specify options for the FileHandler like file pattern, limit of write in bytes, number of files and formatter. The formattter is XML by deafult. However, this type isn't very well arranged for reading. I used SimpleFormatter for tracking exceptions in my client.
After these changes you can run application client again and open log file that is $HOME/javaX.log by default for FileHandler. I realized that a bug isn't in my application but in Glassfish itself. I reported it as the issue 406. I hope that I will win a iPod. :-)
Posted by pblaha
( Mar 15 2006, 05:03:08 PM CET )
Permalink