001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.database;
021
022import org.sonar.api.BatchComponent;
023
024import javax.persistence.EntityManager;
025import javax.persistence.Query;
026import java.util.List;
027
028/**
029 * This component should not accessible from plugin API
030 *
031 * @since 1.10
032 */
033public abstract class DatabaseSession implements BatchComponent {
034
035
036  // IMPORTANT : this value must be the same than the property
037  // hibernate.jdbc.batch_size from /META-INF/persistence.xml (module sonar-database)
038  public static final int BATCH_SIZE = 30;
039
040
041  public abstract EntityManager getEntityManager();
042
043  public abstract void start();
044
045  public abstract void stop();
046
047  public abstract void commit();
048
049  /**
050   * This method should be called before a long period were database will not be accessed
051   * in order to close database connection and avoid timeout. Next use of the
052   * database will automatically open a new connection.
053   */
054  public abstract void commitAndClose();
055
056  public abstract void rollback();
057
058  public abstract <T> T save(T entity);
059
060  public abstract Object saveWithoutFlush(Object entity);
061
062  public abstract boolean contains(Object entity);
063
064  public abstract void save(Object... entities);
065
066  public abstract Object merge(Object entity);
067
068  public abstract void remove(Object entity);
069
070  public abstract void removeWithoutFlush(Object entity);
071
072  public abstract <T> T reattach(Class<T> entityClass, Object primaryKey);
073
074  public abstract Query createQuery(String hql);
075
076  public abstract Query createNativeQuery(String sql);
077
078  public abstract <T> T getSingleResult(Query query, T defaultValue);
079
080  public abstract <T> T getEntity(Class<T> entityClass, Object id);
081
082  public abstract <T> T getSingleResult(Class<T> entityClass, Object... criterias);
083
084  public abstract <T> List<T> getResults(Class<T> entityClass, Object... criterias);
085
086  public abstract <T> List<T> getResults(Class<T> entityClass);
087}