Apart from the C-style formatting, a few unnecessary lines and a bit poor exception handling, you can just do so.
Up vote 1 down vote favorite 3 share g+ share fb share tw.
I need a database connection in Java Web service implemented as a session bean, and I'm not sure if I do it right. I created a class public final class SQLUtils { //..... private static DataSource m_ds=null; static { try { InitialContext ic = new InitialContext(); m_ds = (DataSource) ic. Lookup(dbName); //Connection pool and jdbc resource previously created in Glassfish , dbName contains the proper JNDI resource name } catch (Exception e) { e.printStackTrace(); m_ds = null; } } public static Connection getSQLConnection() throws SQLException { return m_ds.getConnection(); } } Whenever I need a connection I do Connection cn = null; try { cn = SQLUtils.
GetSQLConnection(); // use connection } finally { if (null! = cn) { try { cn.close(); } catch (SQLException e) { } } } Is it ok to use it this way, or I DataSource must be a member of the bean? @Stateless @WebService public class TestBean { private @Resource(name=dbName) DataSource m_ds; } I'm sorry if it is a nube question, but I'm pretty new to Java.
Thanks in advance. Java jdbc java-ee glassfish jndi link|improve this question edited Dec 16 '09 at 17:09BalusC262k27272486 asked Dec 16 '09 at 16:47a1ex0714.7k1717 90% accept rate.
Apart from the C-style formatting, a few unnecessary lines and a bit poor exception handling, you can just do so. Here's how I'd do it: public final class SQLUtil { private static DataSource dataSource; // .. static { try { dataSource = (DataSource) new InitialContext(). Lookup(name); } catch (NamingException e) { throw new ExceptionInInitializerError(e); } } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } } I throw here ExceptionInInitializerError so that the application will immediately stop so that you don't need to face "unexplainable" NullPointerException when trying to obtain a connection.
1 for the ExceptionInInitializerError which I didn't know. – ewernli Dec 16 '09 at 17:22 1 I would however favor the injection in the bean itself because it's easier to mock and to test. – ewernli Dec 16 '09 at 17:24 Thanks a lot for your answer.
– a1ex07 Dec 16 '09 at 17:38 As Pascal pointed out, you actually don't need all that stuff. Just a @Resource is enough. – BalusC Dec 16 '09 at 23:33.
In the ancient J2EE world, the traditional way to manage this was to use a ServiceLocator. Below, a sample implementation (non optimized, the DataSource could be cached): public class ServiceLocator { private Context initalContext; private static ServiceLocator ourInstance = new ServiceLocator(); public static ServiceLocator getInstance() { return ourInstance; } private ServiceLocator() { try { this. InitalContext = new InitialContext(); } catch (NamingException ex) { throw new ServiceLocatorException(...); } } public DataSource getDataSource(String dataSourceName) { DataSource datasource = null; try { Context ctx = (Context) initalContext.
Lookup("java:comp/env"); datasource = (DataSource) ctx. Lookup(dataSourceName); } catch (NamingException ex) { throw new ServiceLocatorException(...); } return datasource; } } To use it, simply call it like this: DataSource ds = ServiceLocator.getInstance(). GetDataSource("jdbc/mydatabase"); But this was prior to the EJB3 and Dependency Injection era.
Now, when using EJB3, if you have setup your DataSource in your EJB container, all you have to do to automatically inject the DataSource in your Stateless Bean is to write (where mydatabase is the name of the datasource): @Resource private DataSource mydatabase; Use the name attribute if you want to explicitly, well, set the name: @Resource(name="jdbc/mydatabase") private DataSource dataSource; EJB3 actually make the ServiceLocator pattern obsolete and you should really prefer injection when working with them.
1 Ah drat, I knew it. That's indeed much better than some helper class. +1.
– BalusC Dec 16 '09 at 23:32.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.