001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2012 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.sonar.core.persistence.Database;
023    import org.sonar.core.persistence.DatabaseVersion;
024    import org.sonar.jpa.entity.SchemaMigration;
025    
026    import javax.persistence.EntityManager;
027    
028    public class MemoryDatabaseConnector extends DefaultDatabaseConnector {
029      private int version;
030    
031      public MemoryDatabaseConnector(Database database) {
032        super(database);
033        version = DatabaseVersion.LAST_VERSION;
034      }
035    
036      @Override
037      public void start() {
038        super.start();
039        setupSchemaVersion(version);
040      }
041    
042      protected void setupSchemaVersion(int version) {
043        SchemaMigration migration = new SchemaMigration();
044        migration.setVersion(version);
045        EntityManager manager = null;
046        try {
047          manager = createEntityManager();
048          manager.getTransaction().begin();
049          manager.persist(migration);
050          manager.getTransaction().commit();
051    
052        } finally {
053          if (manager != null) {
054            manager.close();
055          }
056        }
057      }
058    }