| | |
| | | * @throws NamingException when JNDI lookup fails |
| | | */ |
| | | public void setDataSourceName(String dataSourceName) throws NamingException { |
| | | logger.finest(dataSourceName); |
| | | Context initCtx = new InitialContext(); |
| | | Context envCtx = (Context) initCtx.lookup("java:comp/env"); |
| | | ds = (DataSource) envCtx.lookup(dataSourceName); |
| | |
| | | /** |
| | | * Get a database connection |
| | | * @return a database connection |
| | | * @throws SQLException |
| | | */ |
| | | public Connection getConnection() { |
| | | Connection c = null; |
| | | try { |
| | | if (dburl != null) { |
| | | logger.finest("getting Connection for URL " + dburl); |
| | | c = DriverManager.getConnection(dburl); |
| | | } else if(ds != null) { |
| | | logger.finest("getting Connection for DataSource " + ds.toString()); |
| | | c = ds.getConnection(); |
| | | } else { |
| | | //throw new SQLException("Unable to get connection, DataSource and database URL are null."); |
| | | logger.finest("Unable to get connection, DataSource and database URL are null."); |
| | | } |
| | | } catch(Exception ex) { |
| | | ex.printStackTrace(); |
| | | //ex.printStackTrace(); |
| | | logger.log(Level.FINEST, ex.getLocalizedMessage(), ex); |
| | | } |
| | | return c; |
| | | } |
| | |
| | | try { |
| | | c.setAutoCommit(false); |
| | | } catch(SQLException ex) { |
| | | logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
| | | logger.log(Level.SEVERE, ex.getMessage(), ex); |
| | | } finally { |
| | | // .. |
| | | } |
| | |
| | | public void commit(Connection c) { |
| | | try { |
| | | c.commit(); |
| | | c.setAutoCommit(true); |
| | | } catch(SQLException ex) { |
| | | logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
| | | logger.log(Level.SEVERE, ex.getMessage(), ex); |
| | | } finally { |
| | | // ... |
| | | try { |
| | | c.setAutoCommit(true); |
| | | } catch (SQLException ex) { |
| | | logger.log(Level.SEVERE, ex.getMessage(), ex); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public void rollback(Connection c) { |
| | | try { |
| | | c.rollback(); |
| | | c.setAutoCommit(true); |
| | | } catch(SQLException ex) { |
| | | logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); |
| | | } finally { |
| | | // ... |
| | | try { |
| | | c.setAutoCommit(true); |
| | | } catch (SQLException ex) { |
| | | logger.log(Level.SEVERE, ex.getMessage(), ex); |
| | | } |
| | | } |
| | | } |
| | | |