001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.jpa.session;
021
022 import org.apache.commons.configuration.Configuration;
023 import org.apache.commons.configuration.PropertiesConfiguration;
024 import org.sonar.api.database.DatabaseProperties;
025 import org.sonar.jpa.entity.SchemaMigration;
026
027 import javax.persistence.EntityManager;
028 import javax.persistence.EntityManagerFactory;
029 import java.sql.Connection;
030
031 public class MemoryDatabaseConnector extends DriverDatabaseConnector {
032 public static final String DRIVER = "org.hsqldb.jdbcDriver";
033 public static final String URL = "jdbc:hsqldb:mem:sonar";
034 public static final String USER = "sa";
035 public static final String PASSWORD = "";
036 public static final int ISOLATION = Connection.TRANSACTION_READ_UNCOMMITTED;
037
038 private int version;
039
040 public MemoryDatabaseConnector(Configuration config) {
041 super(config);
042 version = SchemaMigration.LAST_VERSION;
043 }
044
045 public MemoryDatabaseConnector() {
046 this(getInMemoryConfiguration(true));
047 }
048
049 public MemoryDatabaseConnector(int version) {
050 this(getInMemoryConfiguration(true));
051 this.version = version;
052 }
053
054 protected static Configuration getInMemoryConfiguration(boolean createSchema) {
055 PropertiesConfiguration conf = new PropertiesConfiguration();
056 conf.setProperty(DatabaseProperties.PROP_URL, URL);
057 conf.setProperty(DatabaseProperties.PROP_DRIVER, DRIVER);
058 conf.setProperty(DatabaseProperties.PROP_USER, USER);
059 conf.setProperty(DatabaseProperties.PROP_PASSWORD, PASSWORD);
060 conf.setProperty(DatabaseProperties.PROP_ISOLATION, ISOLATION);
061 if (createSchema) {
062 conf.setProperty(DatabaseProperties.PROP_HIBERNATE_HBM2DLL, "create-drop");
063 }
064 return conf;
065 }
066
067 @Override
068 public void start() {
069 try {
070 super.start();
071 } catch (DatabaseException ex) {
072 if (!isStarted()) {
073 throw ex;
074 }
075 setEntityManagerFactory(createEntityManagerFactory());
076 setupSchemaVersion(version);
077 }
078 }
079
080 @Override
081 protected EntityManagerFactory createEntityManagerFactory() {
082 return super.createEntityManagerFactory();
083 }
084
085 protected void setupSchemaVersion(int version) {
086 SchemaMigration migration = new SchemaMigration();
087 migration.setVersion(version);
088 EntityManager manager = null;
089 try {
090 manager = createEntityManager();
091 manager.getTransaction().begin();
092 manager.persist(migration);
093 manager.getTransaction().commit();
094
095 } finally {
096 if (manager != null) {
097 manager.close();
098 }
099 }
100 }
101 }