# HG changeset patch # User Sascha L. Teichmann # Date 1299854478 0 # Node ID 86a1bd9cc50ef011686449b4e12925f167818877 # Parent 15d515fe15f5a826ef46150c0fb395505ae2132d More Hibernate/JPA stuff flys-backend/trunk@1458 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/ChangeLog --- a/flys-backend/ChangeLog Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/ChangeLog Fri Mar 11 14:41:18 2011 +0000 @@ -1,3 +1,27 @@ +2011-03-11 Sascha L. Teichmann + + * doc/schema/postgresql-cleanup.sql: New. Tear down schema + for a postgres database. + + * doc/schema/postgresql.sql: Added squence for + auto generating ids in river table. Cleaned up schema. + + * src/main/java/de/intevation/flys/App.java: Simple + test app to interact with hibernate. Needs to be removed + because its only a toy. + + * src/main/java/de/intevation/flys/utils/DBCPConnectionProvider.java: + New. Binds Apache Commons to Hibernate. + + * pom.xml: Added dependencies to log4j, commons dbcp, + JPA of hibernate. + + * src/main/java/de/intevation/flys/model/River.java: Added + JPA annotations. + + * src/main/java/de/intevation/flys/model/*.java: Replaced + Long with Integer because column ids are only four bytes wide. + 2011-03-11 Sascha L. Teichmann * doc/schema/sqlite.sql, doc/schema/postgresql.sql: Fixed diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/doc/schema/postgresql.sql --- a/flys-backend/doc/schema/postgresql.sql Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/doc/schema/postgresql.sql Fri Mar 11 14:41:18 2011 +0000 @@ -1,22 +1,24 @@ BEGIN; -- Gewaesser +CREATE SEQUENCE RIVERS_ID_SEQ; + CREATE TABLE rivers ( id int PRIMARY KEY NOT NULL, - name VARCHAR(256) NOT NULL UNIQUE + name VARCHAR(256) NOT NULL UNIQUE ); -- Bruecke, Haefen, etc. CREATE TABLE attributes ( id int PRIMARY KEY NOT NULL, - value VARCHAR(256) NOT NULL UNIQUE + value VARCHAR(256) NOT NULL UNIQUE ); -- segments from/to at a river CREATE TABLE ranges ( id int PRIMARY KEY NOT NULL, - river_id int NOT NULL REFERENCES rivers(id), - a NUMERIC NOT NULL, + river_id int NOT NULL REFERENCES rivers(id), + a NUMERIC NOT NULL, b NUMERIC, UNIQUE (river_id, a, b) ); @@ -24,30 +26,30 @@ -- Lage 'links', 'rechts', etc. CREATE TABLE positions ( id int PRIMARY KEY NOT NULL, - value VARCHAR(256) NOT NULL UNIQUE + value VARCHAR(256) NOT NULL UNIQUE ); -- Some object (eg. Hafen) at a segment of river -- plus its position. CREATE TABLE annotations ( id int PRIMARY KEY NOT NULL, - range_id int NOT NULL REFERENCES ranges(id), - attribute_id int NOT NULL REFERENCES attributes(id), + range_id int NOT NULL REFERENCES ranges(id), + attribute_id int NOT NULL REFERENCES attributes(id), position_id int REFERENCES positions(id) ); -- Pegel CREATE TABLE gauges ( - id int PRIMARY KEY NOT NULL, - name VARCHAR(256) NOT NULL, - river_id int NOT NULL REFERENCES rivers(id), - station NUMERIC NOT NULL UNIQUE, - aeo NUMERIC NOT NULL, + id int PRIMARY KEY NOT NULL, + name VARCHAR(256) NOT NULL, + river_id int NOT NULL REFERENCES rivers(id), + station NUMERIC NOT NULL UNIQUE, + aeo NUMERIC NOT NULL, -- Pegelnullpunkt - datum NUMERIC NOT NULL, + datum NUMERIC NOT NULL, -- Streckengueltigkeit - range_id int NOT NULL REFERENCES ranges (id), + range_id int NOT NULL REFERENCES ranges (id), UNIQUE (name, river_id), UNIQUE (river_id, datum) @@ -55,32 +57,32 @@ -- Type of a Hauptwert 'W', 'Q', 'D', etc. CREATE TABLE main_value_types ( - id int PRIMARY KEY NOT NULL, - name VARCHAR(256) NOT NULL UNIQUE + id int PRIMARY KEY NOT NULL, + name VARCHAR(256) NOT NULL UNIQUE ); -- Named type of a Hauptwert (eg. HQ100) CREATE TABLE named_main_values ( - id int PRIMARY KEY NOT NULL, - name VARCHAR(256) NOT NULL UNIQUE, - type_id int NOT NULL REFERENCES main_value_types(id), + id int PRIMARY KEY NOT NULL, + name VARCHAR(256) NOT NULL UNIQUE, + type_id int NOT NULL REFERENCES main_value_types(id), UNIQUE (name, type_id) ); -- Table for time intervals CREATE TABLE time_intervals ( id int PRIMARY KEY NOT NULL, - start_time TIMESTAMP NOT NULL, + start_time TIMESTAMP NOT NULL, stop_time TIMESTAMP, CHECK (start_time <= stop_time) ); -- Stammdaten CREATE TABLE main_values ( - id int PRIMARY KEY NOT NULL, - gauge_id int NOT NULL REFERENCES gauges(id), - named_value_id int NOT NULL REFERENCES named_main_values(id), - value NUMERIC NOT NULL, + id int PRIMARY KEY NOT NULL, + gauge_id int NOT NULL REFERENCES gauges(id), + named_value_id int NOT NULL REFERENCES named_main_values(id), + value NUMERIC NOT NULL, time_interval_id int REFERENCES time_intervals(id), @@ -101,29 +103,29 @@ -- Values of the Abflusstafeln CREATE TABLE discharge_table_values ( - id int PRIMARY KEY NOT NULL, - table_id int NOT NULL REFERENCES discharge_tables(id), - q NUMERIC NOT NULL, - w NUMERIC NOT NULL, + id int PRIMARY KEY NOT NULL, + table_id int NOT NULL REFERENCES discharge_tables(id), + q NUMERIC NOT NULL, + w NUMERIC NOT NULL, UNIQUE (table_id, q, w) ); -- WST files CREATE TABLE wst ( - id int PRIMARY KEY NOT NULL, - river_id int NOT NULL REFERENCES rivers(id), - description VARCHAR(256) NOT NULL, + id int PRIMARY KEY NOT NULL, + river_id int NOT NULL REFERENCES rivers(id), + description VARCHAR(256) NOT NULL, -- TODO: more meta infos UNIQUE (river_id, description) ); -- columns of WST files CREATE TABLE wst_column ( - id int PRIMARY KEY NOT NULL, - wst_id int NOT NULL REFERENCES wst(id), - name VARCHAR(256) NOT NULL, - description VARCHAR, + id int PRIMARY KEY NOT NULL, + wst_id int NOT NULL REFERENCES wst(id), + name VARCHAR(256) NOT NULL, + description VARCHAR, time_interval_id int REFERENCES time_intervals(id), @@ -132,10 +134,10 @@ -- w values in WST file column CREATE TABLE wst_column_values ( - id int PRIMARY KEY NOT NULL, - wst_column_id int NOT NULL REFERENCES wst_column(id), - position NUMERIC NOT NULL, - w NUMERIC NOT NULL, + id int PRIMARY KEY NOT NULL, + wst_column_id int NOT NULL REFERENCES wst_column(id), + position NUMERIC NOT NULL, + w NUMERIC NOT NULL, UNIQUE (position, wst_column_id), UNIQUE (position, wst_column_id, w) @@ -143,9 +145,9 @@ -- bind q values to range CREATE TABLE wst_q_ranges ( - id int PRIMARY KEY NOT NULL, - range_id int NOT NULL REFERENCES ranges(id), - q NUMERIC NOT NULL + id int PRIMARY KEY NOT NULL, + range_id int NOT NULL REFERENCES ranges(id), + q NUMERIC NOT NULL ); -- bind q ranges to wst columns diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/pom.xml --- a/flys-backend/pom.xml Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/pom.xml Fri Mar 11 14:41:18 2011 +0000 @@ -28,6 +28,15 @@ --> + + org.apache.maven.plugins + maven-compiler-plugin + 2.0.2 + + 1.5 + 1.5 + + @@ -41,7 +50,22 @@ org.hibernate hibernate-core - 3.6.1.Final + 3.6.2.Final + + + org.hibernate + hibernate-entitymanager + 3.6.2.Final + + + log4j + log4j + 1.2.14 + + + commons-dbcp + commons-dbcp + 1.2.2 diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/App.java --- a/flys-backend/src/main/java/de/intevation/flys/App.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/App.java Fri Mar 11 14:41:18 2011 +0000 @@ -1,13 +1,37 @@ package de.intevation.flys; -/** - * Hello world! - * - */ +import org.hibernate.cfg.Configuration; + +import org.hibernate.SessionFactory; + +import de.intevation.flys.model.River; + +import org.hibernate.dialect.resolver.DialectFactory; + +import org.hibernate.dialect.Dialect; + public class App { - public static void main( String[] args ) + public static void main(String [] args) + throws Exception { - System.out.println( "Hello World!" ); + Configuration cfg = new Configuration(); + + System.err.println(cfg); + + cfg.addAnnotatedClass(River.class); + + String [] setupScript = cfg.generateSchemaCreationScript( + DialectFactory.constructDialect( + "org.hibernate.dialect.PostgreSQLDialect")); + + for (String line: setupScript) { + System.out.println(line); + } + + + //SessionFactory sessionFactory = cfg.buildSessionFactory(); + } } +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/Annotation.java --- a/flys-backend/src/main/java/de/intevation/flys/model/Annotation.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/Annotation.java Fri Mar 11 14:41:18 2011 +0000 @@ -5,7 +5,7 @@ public class Annotation implements Serializable { - private Long id; + private Integer id; private Range range; private Attribute attribute; private Position position; @@ -13,11 +13,11 @@ public Annotation() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/Attribute.java --- a/flys-backend/src/main/java/de/intevation/flys/model/Attribute.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/Attribute.java Fri Mar 11 14:41:18 2011 +0000 @@ -5,18 +5,18 @@ public class Attribute implements Serializable { - private Long id; + private Integer id; private String value; public Attribute() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/DischargeTable.java --- a/flys-backend/src/main/java/de/intevation/flys/model/DischargeTable.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/DischargeTable.java Fri Mar 11 14:41:18 2011 +0000 @@ -5,18 +5,18 @@ public class DischargeTable implements Serializable { - private Long id; + private Integer id; private Gauge gauge; private TimeInterval timeInterval; public DischargeTable() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/DischargeTableValue.java --- a/flys-backend/src/main/java/de/intevation/flys/model/DischargeTableValue.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/DischargeTableValue.java Fri Mar 11 14:41:18 2011 +0000 @@ -7,7 +7,7 @@ public class DischargeTableValue implements Serializable { - private Long id; + private Integer id; private DischargeTable dischargeTable; private BigDecimal q; private BigDecimal w; @@ -15,11 +15,11 @@ public DischargeTableValue() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/Gauge.java --- a/flys-backend/src/main/java/de/intevation/flys/model/Gauge.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/Gauge.java Fri Mar 11 14:41:18 2011 +0000 @@ -7,7 +7,7 @@ public class Gauge implements Serializable { - private Long id; + private Integer id; private String name; private River river; private BigDecimal station; @@ -18,11 +18,11 @@ public Gauge() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/MainValueType.java --- a/flys-backend/src/main/java/de/intevation/flys/model/MainValueType.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/MainValueType.java Fri Mar 11 14:41:18 2011 +0000 @@ -5,18 +5,18 @@ public class MainValueType implements Serializable { - private Long id; + private Integer id; private String value; public MainValueType() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/NamedMainValues.java --- a/flys-backend/src/main/java/de/intevation/flys/model/NamedMainValues.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/NamedMainValues.java Fri Mar 11 14:41:18 2011 +0000 @@ -5,18 +5,18 @@ public class NamedMainValues implements Serializable { - private Long id; + private Integer id; private String name; private MainValueType type; public NamedMainValues() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/Position.java --- a/flys-backend/src/main/java/de/intevation/flys/model/Position.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/Position.java Fri Mar 11 14:41:18 2011 +0000 @@ -5,18 +5,18 @@ public class Position implements Serializable { - private Long id; + private Integer id; private String value; public Position() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/Range.java --- a/flys-backend/src/main/java/de/intevation/flys/model/Range.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/Range.java Fri Mar 11 14:41:18 2011 +0000 @@ -7,7 +7,7 @@ public class Range implements Serializable { - private Long id; + private Integer id; private BigDecimal a; private BigDecimal b; @@ -16,11 +16,11 @@ public Range() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/River.java --- a/flys-backend/src/main/java/de/intevation/flys/model/River.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/River.java Fri Mar 11 14:41:18 2011 +0000 @@ -2,29 +2,49 @@ import java.io.Serializable; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.GeneratedValue; +import javax.persistence.Column; +import javax.persistence.SequenceGenerator; + +import javax.persistence.GenerationType; + +@Entity +@Table(name = "rivers") public class River implements Serializable { - private Long id; + private Integer id; - private String name; + private String name; - public void setId(Long id) { + @Id + @SequenceGenerator( + name = "SEQUENCE RIVERS_ID_SEQ", + sequenceName = "RIVERS_ID_SEQ") + @GeneratedValue( + strategy = GenerationType.SEQUENCE, + generator = "SEQUENCE RIVERS_ID_SEQ") + @Column(name = "id") + public Integer getId() { + return id; + } + + public void setId(Integer id) { this.id = id; } - public Long getId() { - return id; + @Column(name = "name") + public String getName() { + return name; } public void setName(String name) { this.name = name; } - public String getName() { - return name; - } - public River() { } } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/TimeInterval.java --- a/flys-backend/src/main/java/de/intevation/flys/model/TimeInterval.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/TimeInterval.java Fri Mar 11 14:41:18 2011 +0000 @@ -7,18 +7,18 @@ public class TimeInterval implements Serializable { - private Long id; + private Integer id; private Date startTime; private Date stopTime; public TimeInterval() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/Wst.java --- a/flys-backend/src/main/java/de/intevation/flys/model/Wst.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/Wst.java Fri Mar 11 14:41:18 2011 +0000 @@ -5,18 +5,18 @@ public class Wst implements Serializable { - private Long id; + private Integer id; private River river; private String description; public Wst() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/WstColumn.java --- a/flys-backend/src/main/java/de/intevation/flys/model/WstColumn.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/WstColumn.java Fri Mar 11 14:41:18 2011 +0000 @@ -5,7 +5,7 @@ public class WstColumn implements Serializable { - private Long id; + private Integer id; private Wst wst; private String name; private String description; @@ -14,11 +14,11 @@ public WstColumn() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/WstColumnQRange.java --- a/flys-backend/src/main/java/de/intevation/flys/model/WstColumnQRange.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/WstColumnQRange.java Fri Mar 11 14:41:18 2011 +0000 @@ -1,24 +1,22 @@ package de.intevation.flys.model; -import java.math.BigDecimal; - import java.io.Serializable; public class WstColumnQRange implements Serializable { - private Long id; + private Integer id; private WstColumn wstColumn; private WstQRange wstQRange; public WstColumnQRange() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/WstColumnValue.java --- a/flys-backend/src/main/java/de/intevation/flys/model/WstColumnValue.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/WstColumnValue.java Fri Mar 11 14:41:18 2011 +0000 @@ -7,7 +7,7 @@ public class WstColumnValue implements Serializable { - private Long id; + private Integer id; private WstColumn wstColumn; private Position position; private BigDecimal w; @@ -15,11 +15,11 @@ public WstColumnValue() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/model/WstQRange.java --- a/flys-backend/src/main/java/de/intevation/flys/model/WstQRange.java Thu Mar 10 13:44:51 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/WstQRange.java Fri Mar 11 14:41:18 2011 +0000 @@ -7,18 +7,18 @@ public class WstQRange implements Serializable { - private Long id; + private Integer id; private Range range; private BigDecimal q; public WstQRange() { } - public void setId(Long id) { + public void setId(Integer id) { this.id = id; } - public Long getId() { + public Integer getId() { return id; } diff -r 15d515fe15f5 -r 86a1bd9cc50e flys-backend/src/main/java/de/intevation/flys/utils/DBCPConnectionProvider.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/utils/DBCPConnectionProvider.java Fri Mar 11 14:41:18 2011 +0000 @@ -0,0 +1,245 @@ +/* + * Copyright 2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.hibernate.connection; + +import java.io.PrintWriter; +import java.io.StringWriter; + +import java.sql.Connection; +import java.sql.SQLException; + +import java.util.Iterator; +import java.util.Properties; +import java.util.Map; + +import org.apache.commons.dbcp.BasicDataSource; +import org.apache.commons.dbcp.BasicDataSourceFactory; + +import org.apache.log4j.Logger; + +import org.hibernate.HibernateException; + +import org.hibernate.connection.ConnectionProviderFactory; +import org.hibernate.connection.ConnectionProvider; + +import org.hibernate.cfg.Environment; + +/** + *

A connection provider that uses an Apache commons DBCP connection pool.

+ * + *

To use this connection provider set:
+ * hibernate.connection.provider_class org.hibernate.connection.DBCPConnectionProvider

+ * + *
Supported Hibernate properties:
+ *   hibernate.connection.driver_class
+ *   hibernate.connection.url
+ *   hibernate.connection.username
+ *   hibernate.connection.password
+ *   hibernate.connection.isolation
+ *   hibernate.connection.autocommit
+ *   hibernate.connection.pool_size
+ *   hibernate.connection (JDBC driver properties)
+ *
+ * All DBCP properties are also supported by using the hibernate.dbcp prefix. + * A complete list can be found on the DBCP configuration page: + * http://jakarta.apache.org/commons/dbcp/configuration.html. + *
+ *
Example:
+ *   hibernate.connection.provider_class org.hibernate.connection.DBCPConnectionProvider
+ *   hibernate.connection.driver_class org.hsqldb.jdbcDriver
+ *   hibernate.connection.username sa
+ *   hibernate.connection.password
+ *   hibernate.connection.url jdbc:hsqldb:test
+ *   hibernate.connection.pool_size 20
+ *   hibernate.dbcp.initialSize 10
+ *   hibernate.dbcp.maxWait 3000
+ *   hibernate.dbcp.validationQuery select 1 from dual
+ * + *

More information about configuring/using DBCP can be found on the + * DBCP website. + * There you will also find the DBCP wiki, mailing lists, issue tracking + * and other support facilities

+ * + * @see org.hibernate.connection.ConnectionProvider + * @author Dirk Verbeeck + */ +public class DBCPConnectionProvider +implements ConnectionProvider +{ + private static Logger log = Logger.getLogger(DBCPConnectionProvider.class); + + private static final String PREFIX = "hibernate.dbcp."; + + private BasicDataSource ds; + + // Old Environment property for backward-compatibility + // (property removed in Hibernate3) + private static final String DBCP_PS_MAXACTIVE = + "hibernate.dbcp.ps.maxActive"; + + // Property doesn't exists in Hibernate2 + private static final String AUTOCOMMIT = + "hibernate.connection.autocommit"; + + public void configure(Properties props) throws HibernateException { + try { + log.debug("Configure DBCPConnectionProvider"); + + // DBCP properties used to create the BasicDataSource + Properties dbcpProperties = new Properties(); + + // DriverClass & url + String jdbcDriverClass = props.getProperty(Environment.DRIVER); + String jdbcUrl = props.getProperty(Environment.URL); + dbcpProperties.put("driverClassName", jdbcDriverClass); + dbcpProperties.put("url", jdbcUrl); + + // Username / password + String username = props.getProperty(Environment.USER); + String password = props.getProperty(Environment.PASS); + dbcpProperties.put("username", username); + dbcpProperties.put("password", password); + + // Isolation level + String isolationLevel = props.getProperty(Environment.ISOLATION); + if (isolationLevel != null + && (isolationLevel = isolationLevel.trim()).length() > 0) { + dbcpProperties.put("defaultTransactionIsolation", isolationLevel); + } + + // Turn off autocommit (unless autocommit property is set) + String autocommit = props.getProperty(AUTOCOMMIT); + if (autocommit != null + && (autocommit = autocommit.trim()).length() > 0) { + dbcpProperties.put("defaultAutoCommit", autocommit); + } else { + dbcpProperties.put("defaultAutoCommit", String.valueOf(Boolean.FALSE)); + } + + // Pool size + String poolSize = props.getProperty(Environment.POOL_SIZE); + if (poolSize != null + && (poolSize = poolSize.trim()).length() > 0 + && Integer.parseInt(poolSize) > 0) { + dbcpProperties.put("maxActive", poolSize); + } + + // Copy all "driver" properties into "connectionProperties" + Properties driverProps = + ConnectionProviderFactory.getConnectionProperties(props); + + if (driverProps.size() > 0) { + StringBuilder connectionProperties = new StringBuilder(); + for (Iterator iter = driverProps.entrySet().iterator(); + iter.hasNext(); + ) { + Map.Entry entry = (Map.Entry)iter.next(); + String key = (String)entry.getKey(); + String value = (String)entry.getValue(); + connectionProperties + .append(key) + .append('=') + .append(value); + if (iter.hasNext()) { + connectionProperties.append(';'); + } + } + dbcpProperties.put( + "connectionProperties", connectionProperties.toString()); + } + + // Copy all DBCP properties removing the prefix + for (Iterator iter = props.entrySet().iterator() ; iter.hasNext() ;) { + Map.Entry entry = (Map.Entry)iter.next(); + String key = (String)entry.getKey(); + if (key.startsWith(PREFIX)) { + String property = key.substring(PREFIX.length()); + String value = (String)entry.getValue(); + dbcpProperties.put(property, value); + } + } + + // Backward-compatibility + if (props.getProperty(DBCP_PS_MAXACTIVE) != null) { + dbcpProperties.put( + "poolPreparedStatements", + String.valueOf(Boolean.TRUE)); + dbcpProperties.put( + "maxOpenPreparedStatements", + props.getProperty(DBCP_PS_MAXACTIVE)); + } + + // Some debug info + if (log.isDebugEnabled()) { + log.debug("Creating a DBCP BasicDataSource" + + " with the following DBCP factory properties:"); + StringWriter sw = new StringWriter(); + dbcpProperties.list(new PrintWriter(sw, true)); + log.debug(sw.toString()); + } + + // Let the factory create the pool + ds = (BasicDataSource)BasicDataSourceFactory + .createDataSource(dbcpProperties); + + // The BasicDataSource has lazy initialization + // borrowing a connection will start the DataSource + // and make sure it is configured correctly. + + // Connection conn = ds.getConnection(); + // conn.close(); + } + catch (Exception e) { + String message = "Could not create a DBCP pool"; + log.fatal(message, e); + if (ds != null) { + BasicDataSource x = ds; ds = null; + try { + x.close(); + } + catch (SQLException sqle) { + } + } + throw new HibernateException(message, e); + } + log.debug("Configure DBCPConnectionProvider complete"); + } + + public Connection getConnection() throws SQLException { + return ds.getConnection(); + } + + public void closeConnection(Connection conn) throws SQLException { + conn.close(); + } + + public void close() throws HibernateException { + try { + if (ds != null) { + BasicDataSource x = ds; ds = null; + x.close(); + } + } + catch (SQLException sqle) { + throw new HibernateException("Could not close DBCP pool", sqle); + } + } + + public boolean supportsAggressiveRelease() { + return false; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :