diff options
Diffstat (limited to 'src/com/vaadin/data/util/connection/J2EEConnectionPool.java')
-rw-r--r-- | src/com/vaadin/data/util/connection/J2EEConnectionPool.java | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/com/vaadin/data/util/connection/J2EEConnectionPool.java b/src/com/vaadin/data/util/connection/J2EEConnectionPool.java new file mode 100644 index 0000000000..789fcb9225 --- /dev/null +++ b/src/com/vaadin/data/util/connection/J2EEConnectionPool.java @@ -0,0 +1,61 @@ +package com.vaadin.data.util.connection; + +import java.sql.Connection; +import java.sql.SQLException; + +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; + +public class J2EEConnectionPool implements JDBCConnectionPool { + + private String dataSourceJndiName; + + private DataSource dataSource = null; + + public J2EEConnectionPool(DataSource dataSource) { + this.dataSource = dataSource; + } + + public J2EEConnectionPool(String dataSourceJndiName) { + this.dataSourceJndiName = dataSourceJndiName; + } + + public Connection reserveConnection() throws SQLException { + Connection conn = getDataSource().getConnection(); + conn.setAutoCommit(false); + + return conn; + } + + private DataSource getDataSource() throws SQLException { + if (dataSource == null) { + dataSource = lookupDataSource(); + } + return dataSource; + } + + private DataSource lookupDataSource() throws SQLException { + try { + InitialContext ic = new InitialContext(); + return (DataSource) ic.lookup(dataSourceJndiName); + } catch (NamingException e) { + throw new SQLException( + "NamingException - Cannot connect to the database. Cause: " + + e.getMessage()); + } + } + + public void releaseConnection(Connection conn) { + try { + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void destroy() { + dataSource = null; + } + +}
\ No newline at end of file |