001: /*
002: This file is part of the PolePosition database benchmark
003: http://www.polepos.org
004:
005: This program is free software; you can redistribute it and/or
006: modify it under the terms of the GNU General Public License
007: as published by the Free Software Foundation; either version 2
008: of the License, or (at your option) any later version.
009:
010: This program is distributed in the hope that it will be useful,
011: but WITHOUT ANY WARRANTY; without even the implied warranty of
012: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: GNU General Public License for more details.
014:
015: You should have received a copy of the GNU General Public
016: License along with this program; if not, write to the Free
017: Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
018: MA 02111-1307, USA. */
019:
020: package org.polepos.teams.hibernate;
021:
022: import org.polepos.framework.*;
023: import org.polepos.teams.hibernate.data.*;
024: import org.polepos.teams.jdbc.*;
025:
026: import net.sf.hibernate.*;
027: import net.sf.hibernate.Session;
028: import net.sf.hibernate.cfg.*;
029: import net.sf.hibernate.tool.hbm2ddl.*;
030:
031: /**
032: *
033: * @author Herkules
034: */
035: public class HibernateCar extends Car {
036:
037: private SessionFactory mFactory;
038:
039: private final String mDBType;
040:
041: private Session mSession;
042:
043: public HibernateCar(String dbType) {
044: mDBType = dbType;
045: }
046:
047: public String name() {
048: return mDBType;
049: }
050:
051: public void openSession() throws CarMotorFailureException {
052:
053: if (mFactory == null) {
054: mFactory = getSessionFactory();
055: }
056:
057: assert mSession == null;
058:
059: try {
060: mSession = mFactory.openSession();
061: } catch (HibernateException e) {
062: e.printStackTrace();
063: throw new CarMotorFailureException();
064: }
065: }
066:
067: public void closeSession() {
068:
069: if (mSession != null) {
070: try {
071: mSession.close();
072: } catch (HibernateException e) {
073: e.printStackTrace();
074: }
075: mSession = null;
076: }
077:
078: }
079:
080: public Session getSession() {
081: return mSession;
082: }
083:
084: /**
085: *
086: */
087: private SessionFactory getSessionFactory() {
088: try {
089: Configuration cfg = new Configuration().addClass(
090: HibernatePilot.class).addClass(HibernateTree.class)
091: .addClass(HibernateIndexedPilot.class).addClass(
092: HB0.class);
093:
094: try {
095: Class.forName(Jdbc.settings().getDriverClass(mDBType))
096: .newInstance();
097: } catch (Exception ex) {
098: ex.printStackTrace();
099: }
100:
101: cfg.setProperty("hibernate.connection.url", Jdbc.settings()
102: .getConnectUrl(mDBType));
103:
104: String user = Jdbc.settings().getUsername(mDBType);
105: if (user != null) {
106: cfg.setProperty("hibernate.connection.user", user);
107: }
108:
109: String password = Jdbc.settings().getPassword(mDBType);
110: if (password != null) {
111: cfg.setProperty("hibernate.connection.password",
112: password);
113: }
114:
115: String dialect = Jdbc.settings().getHibernateDialect(
116: mDBType);
117: if (dialect != null) {
118: cfg.setProperty("hibernate.dialect", dialect);
119: }
120:
121: cfg.setProperty("hibernate.query.substitutions",
122: "true 1, false 0, yes 'Y', no 'N'");
123: cfg.setProperty("hibernate.connection.pool_size", "1");
124: cfg.setProperty("hibernate.proxool.pool_alias", "pool1");
125: cfg.setProperty("hibernate.jdbc.batch_size", "0");
126: cfg.setProperty("hibernate.jdbc.batch_versioned_data",
127: "true");
128: cfg.setProperty("hibernate.jdbc.use_streams_for_binary",
129: "true");
130: cfg.setProperty("hibernate.max_fetch_depth", "1");
131: cfg.setProperty("hibernate.cache.region_prefix",
132: "hibernate.test");
133: cfg.setProperty("hibernate.cache.use_query_cache", "true");
134: cfg.setProperty("hibernate.cache.provider_class",
135: "net.sf.hibernate.cache.EhCacheProvider");
136:
137: cfg.setProperty("hibernate.proxool.pool_alias", "pool1");
138: cfg.setProperty("hibernate.proxool.pool_alias", "pool1");
139:
140: SessionFactory factory = cfg.buildSessionFactory();
141: new SchemaExport(cfg).create(true, true);
142: return factory;
143: } catch (MappingException mex) {
144: mex.printStackTrace();
145: } catch (HibernateException hex) {
146: hex.printStackTrace();
147: }
148: return null;
149: }
150:
151: }
|