Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
Jazz, here's a lightweight package that so far has met all of my needs: http://butterfly.jenkov.com/ ... I'll be sure to take a look. Thanks for the response. Jazz - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
Jazz, here's a lightweight package that so far has met all of my needs: http://butterfly.jenkov.com/ ... Cheers, Bill* * On Thu, Jan 7, 2010 at 4:30 PM, Jasvinder S. Bahra < bbdl21...@blueyonder.co.uk> wrote: > I'll echo the Spring suggestion. It simplifies Action-level programming by >> quite a bit. >> >> On the flip side, it requires some extra XML configuration, but IMO it's a >> good tradeoff. >> > > Spring seems to be quite popular here. I'm somewhat reluctant because > Spring provides a lot more than just Database Injection - and for the time > being, DI is all I really want. > > If I cant find what I want, I guess i'll have to bite the bullet and jump > into it. > > Thanks for the response. > > > Jazz > > > > > - > To unsubscribe, e-mail: user-unsubscr...@struts.apache.org > For additional commands, e-mail: user-h...@struts.apache.org > >
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
I'll echo the Spring suggestion. It simplifies Action-level programming by quite a bit. On the flip side, it requires some extra XML configuration, but IMO it's a good tradeoff. Spring seems to be quite popular here. I'm somewhat reluctant because Spring provides a lot more than just Database Injection - and for the time being, DI is all I really want. If I cant find what I want, I guess i'll have to bite the bullet and jump into it. Thanks for the response. Jazz - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
Good luck, and let us know how it goes. (*Chris*) On Thu, Jan 7, 2010 at 3:44 PM, Jasvinder S. Bahra < bbdl21...@blueyonder.co.uk> wrote: > We use Spring (and Spring-LDAP) for that layer. It works great, manages >> all >> our DAO's and their resources, and then injects them into the Actions as >> needed. >> > > Chris, > > At the moment, i'm trying to reaquaint myself with the ins and out of > Struts (I haven't touched it for several years), so I dont (yet) want to > introduce any additional libraries. > > I'm sure its possible to do what I need without them. > > Thanks for the suggestion anyway. > > > Jazz > > > > > - > To unsubscribe, e-mail: user-unsubscr...@struts.apache.org > For additional commands, e-mail: user-h...@struts.apache.org > >
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
So want your DAO's to be singletons? You would have to make them Thread-safe (or synchronize and kill performance), beware that database connections are not thread-safe. Please note that i'm using using the database connection pooling facilities provided by the Servlet Container (Apache Tomcat). As long as the DAO aquires the database connection through a JNDI datasource, the Servlet Container will take care of the thread and connection management issues. The preferred method is use a DI facility (like Guice or Spring) and instantiate a new DAO each time and inject the database connection into it and inject the DAO into the Struts action. I'd prefer to avoid the use of additional libraries for the time being which is why I haven't taken advantage of the libraries that you mentioned. I'm more than happy to remove the acquiring of the datasource code from the DAO's constructor, and add a method to allow an instantiated version of the class to be assigned a data source. However, I would need a way of acquiring the datasource and assigning it to the DAO *without* using any additional libraries. Any ideas? Thanks for the response. Jazz - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
We use Spring (and Spring-LDAP) for that layer. It works great, manages all our DAO's and their resources, and then injects them into the Actions as needed. Chris, At the moment, i'm trying to reaquaint myself with the ins and out of Struts (I haven't touched it for several years), so I dont (yet) want to introduce any additional libraries. I'm sure its possible to do what I need without them. Thanks for the suggestion anyway. Jazz - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
I'll echo the Spring suggestion. It simplifies Action-level programming by quite a bit. On the flip side, it requires some extra XML configuration, but IMO it's a good tradeoff. -Brian On Thu, Jan 7, 2010 at 12:18 PM, Chris Pratt wrote: > We use Spring (and Spring-LDAP) for that layer. It works great, manages > all > our DAO's and their resources, and then injects them into the Actions as > needed. > (*Chris*) > > On Thu, Jan 7, 2010 at 10:06 AM, Jasvinder S. Bahra < > bbdl21...@blueyonder.co.uk> wrote: > > > Does anyone know of any techniques that would allow a Database Access > > Object (DAO) to be used from the various actions that make up a Struts > > application? > > > > I have a DAO which, when instantiated, acquires a data source from the > > servlet container (in this case, Apache Tomcat). Thereafter, I can > execute > > the various methods provided by the object which use the data source to > > execute SQL queries/statements, and return usable data in the form of > > Strings, Maps, Vectors etc, as appropriate. > > > > Instantiating the DAO in the execute() method of each of my action's > > however, seems a little inefficient. Does Struts provide any way to > > instantiate the object once and then make it available for the lifetime > of > > the container (in a way that my Actions can access it)? > > > > Note please that i'm well aware that this is a somewhat non-standard > > approach to database interaction. I am intending to investigate > Hibernate > > (and like) at some point, but for the time being I want to avoid such. > > > > This is the skeleton of the DAO I have in mind... > > > > -- > > import java.sql.*; > > import javax.sql.*; > > import javax.naming.*; > > import javax.servlet.*; > > > > public class MyDataAccessObject { > > > > private DataSource dataSource; > > private String SQL; > > > > public MyDataAccessObject() throws Exception { > > > > try { > > Context init = new InitialContext(); > > Context ctx = (Context) init.lookup("java:comp/env"); > > dataSource = (DataSource) ctx.lookup("jdbc/mysqldb"); > > } catch (NamingException ne) { > > throw new Exception("Cannot retrieve > java:comp/env/jdbc/mysqldb", > > ne); > > } > > > > SQL = null; > > } > > > > public String getValue(String name) { > > > > Connection dbcon = null; > > PreparedStatement pstmt = null; > > ResultSet rs = null; > > > > String value = null; > > > > try { > > dbcon = dataSource.getConnection(); > > > > SQL = "SELECT value FROM table WHERE name = ?"; > > > > pstmt = dbcon.prepareStatement(SQL); > > pstmt.setString(1, name); > > > > rs = pstmt.executeQuery(); > > while (rs.next()) { > > value = rs.getString("value"); > > } > > > > rs.close(); > > rs = null; > > > > pstmt.close(); > > pstmt = null; > > > > dbcon.close(); > > dbcon = null; > > } catch (SQLException ex) { > > log(ex); > > } catch (Exception ex) { > > log(ex); > > } finally { > > if (rs != null) { > > try { rs.close(); } catch (Exception fe1) { log(fe1); } > > rs = null; > > } > > > > if (pstmt != null) { > > try { pstmt.close(); } catch (Exception fe2) { log(fe2); } > > pstmt = null; > > } > > > > if (dbcon != null) { > > try { dbcon.close(); } catch (Exception fe3) { log(fe3); } > > dbcon = null; > > } > > } > > > > SQL = null; > > return value; > > } > > > > private void log(...) { > > // Handle logging > > } > > } > > -- > > > > Thanks, > > > > Jazz > > > > > > > > > > - > > To unsubscribe, e-mail: user-unsubscr...@struts.apache.org > > For additional commands, e-mail: user-h...@struts.apache.org > > > > >
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
> > Instantiating the DAO in the execute() method of each of my action's > however, seems a little inefficient. Does Struts provide any way to > instantiate the object once and then make it available for the lifetime of > the container (in a way that my Actions can access it)? > So want your DAO's to be singletons? You would have to make them Thread-safe (or synchronize and kill performance), beware that database connections are not thread-safe. Doesn't really sound like a good plan to me. If you just have one DAO of each type (singletons) then you need to ensure they are thread-safe and you need to either handle the acquire/release of database connections within each method or pass in the connection. Neither is a great design, the first make you duplicate fragile difficult to test code in every DAO the second exposes database connections in the DAO methods. It's hard to write DAO's as singletons and get them right, much easier to use a new one each time as if they have no state then instantiating them is cheap (where writing thread safe code is hard, hard to write, hard to test). The preferred method is use a DI facility (like Guice or Spring) and instantiate a new DAO each time and inject the database connection into it and inject the DAO into the Struts action. This make it easy to write, easy to test and you don't have to expose database connections and handling logic to the actions. - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org
Re: Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
We use Spring (and Spring-LDAP) for that layer. It works great, manages all our DAO's and their resources, and then injects them into the Actions as needed. (*Chris*) On Thu, Jan 7, 2010 at 10:06 AM, Jasvinder S. Bahra < bbdl21...@blueyonder.co.uk> wrote: > Does anyone know of any techniques that would allow a Database Access > Object (DAO) to be used from the various actions that make up a Struts > application? > > I have a DAO which, when instantiated, acquires a data source from the > servlet container (in this case, Apache Tomcat). Thereafter, I can execute > the various methods provided by the object which use the data source to > execute SQL queries/statements, and return usable data in the form of > Strings, Maps, Vectors etc, as appropriate. > > Instantiating the DAO in the execute() method of each of my action's > however, seems a little inefficient. Does Struts provide any way to > instantiate the object once and then make it available for the lifetime of > the container (in a way that my Actions can access it)? > > Note please that i'm well aware that this is a somewhat non-standard > approach to database interaction. I am intending to investigate Hibernate > (and like) at some point, but for the time being I want to avoid such. > > This is the skeleton of the DAO I have in mind... > > -- > import java.sql.*; > import javax.sql.*; > import javax.naming.*; > import javax.servlet.*; > > public class MyDataAccessObject { > > private DataSource dataSource; > private String SQL; > > public MyDataAccessObject() throws Exception { > > try { > Context init = new InitialContext(); > Context ctx = (Context) init.lookup("java:comp/env"); > dataSource = (DataSource) ctx.lookup("jdbc/mysqldb"); > } catch (NamingException ne) { > throw new Exception("Cannot retrieve java:comp/env/jdbc/mysqldb", > ne); > } > > SQL = null; > } > > public String getValue(String name) { > > Connection dbcon = null; > PreparedStatement pstmt = null; > ResultSet rs = null; > > String value = null; > > try { > dbcon = dataSource.getConnection(); > > SQL = "SELECT value FROM table WHERE name = ?"; > > pstmt = dbcon.prepareStatement(SQL); > pstmt.setString(1, name); > > rs = pstmt.executeQuery(); > while (rs.next()) { > value = rs.getString("value"); > } > > rs.close(); > rs = null; > > pstmt.close(); > pstmt = null; > > dbcon.close(); > dbcon = null; > } catch (SQLException ex) { > log(ex); > } catch (Exception ex) { > log(ex); > } finally { > if (rs != null) { > try { rs.close(); } catch (Exception fe1) { log(fe1); } > rs = null; > } > > if (pstmt != null) { > try { pstmt.close(); } catch (Exception fe2) { log(fe2); } > pstmt = null; > } > > if (dbcon != null) { > try { dbcon.close(); } catch (Exception fe3) { log(fe3); } > dbcon = null; > } > } > > SQL = null; > return value; > } > > private void log(...) { > // Handle logging > } > } > -- > > Thanks, > > Jazz > > > > > - > To unsubscribe, e-mail: user-unsubscr...@struts.apache.org > For additional commands, e-mail: user-h...@struts.apache.org > >
Struts, Tomcat, JNDI, JDBC without use of an ORM Library such as Hibernate
Does anyone know of any techniques that would allow a Database Access Object (DAO) to be used from the various actions that make up a Struts application? I have a DAO which, when instantiated, acquires a data source from the servlet container (in this case, Apache Tomcat). Thereafter, I can execute the various methods provided by the object which use the data source to execute SQL queries/statements, and return usable data in the form of Strings, Maps, Vectors etc, as appropriate. Instantiating the DAO in the execute() method of each of my action's however, seems a little inefficient. Does Struts provide any way to instantiate the object once and then make it available for the lifetime of the container (in a way that my Actions can access it)? Note please that i'm well aware that this is a somewhat non-standard approach to database interaction. I am intending to investigate Hibernate (and like) at some point, but for the time being I want to avoid such. This is the skeleton of the DAO I have in mind... -- import java.sql.*; import javax.sql.*; import javax.naming.*; import javax.servlet.*; public class MyDataAccessObject { private DataSource dataSource; private String SQL; public MyDataAccessObject() throws Exception { try { Context init = new InitialContext(); Context ctx = (Context) init.lookup("java:comp/env"); dataSource = (DataSource) ctx.lookup("jdbc/mysqldb"); } catch (NamingException ne) { throw new Exception("Cannot retrieve java:comp/env/jdbc/mysqldb", ne); } SQL = null; } public String getValue(String name) { Connection dbcon = null; PreparedStatement pstmt = null; ResultSet rs = null; String value = null; try { dbcon = dataSource.getConnection(); SQL = "SELECT value FROM table WHERE name = ?"; pstmt = dbcon.prepareStatement(SQL); pstmt.setString(1, name); rs = pstmt.executeQuery(); while (rs.next()) { value = rs.getString("value"); } rs.close(); rs = null; pstmt.close(); pstmt = null; dbcon.close(); dbcon = null; } catch (SQLException ex) { log(ex); } catch (Exception ex) { log(ex); } finally { if (rs != null) { try { rs.close(); } catch (Exception fe1) { log(fe1); } rs = null; } if (pstmt != null) { try { pstmt.close(); } catch (Exception fe2) { log(fe2); } pstmt = null; } if (dbcon != null) { try { dbcon.close(); } catch (Exception fe3) { log(fe3); } dbcon = null; } } SQL = null; return value; } private void log(...) { // Handle logging } } -- Thanks, Jazz - To unsubscribe, e-mail: user-unsubscr...@struts.apache.org For additional commands, e-mail: user-h...@struts.apache.org