Package com.tagtraum.beatunes.library
Class HibernateUtil
- java.lang.Object
-
- com.tagtraum.beatunes.library.HibernateUtil
-
public final class HibernateUtil extends Object
HibernateUtil.- Author:
- Hendrik Schreiber
-
-
Constructor Summary
Constructors Constructor Description HibernateUtil()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
closeSession()
Deprecated.static boolean
doesDatabaseExist(File databaseBaseFilename)
Indicates whether a database actually exists.static void
dumpLocks(Connection connection)
void
enableFastImportSettings(boolean enable)
Sets special database settings for the very first import, i.e.<R> R
execute(Function<org.hibernate.Session,R> work)
Execute some database work with a given session.<R> R
execute(Function<org.hibernate.Session,R> work, boolean failSilently)
Execute some database work with a given session.void
executeTransaction(Consumer<org.hibernate.Session> work)
Execute some database work inside a transaction.<R> R
executeTransaction(Function<org.hibernate.Session,R> work)
Execute some database work inside a transaction.static String
getBestIndex(Class<?> entity, String... orderedColumns)
Extracts the best index for the given entity class for the given columns from JPA class annotations.File
getDatabaseBaseDirectory()
Returns the database directory name.File
getDatabaseBaseFilename()
Returns the database base file name.void
handleException(SQLException sqlException)
void
handleException(org.hibernate.JDBCException e)
void
init()
boolean
isRunning()
void
logStatistics()
Log Hibernate statistics, if enabled.void
markDatabaseForDeletion()
void
markDatabaseForRecovery()
org.hibernate.Session
openSession()
Deprecated.void
removeDatabaseLock()
void
setDatabaseBaseFilename(File databaseBaseFilename)
Sets the database base file name.void
setHibernateMediaLibrary(HibernateMediaLibrary<?> hibernateMediaLibrary)
void
shutdown()
void
shutdown(boolean compact)
-
-
-
Method Detail
-
setHibernateMediaLibrary
public void setHibernateMediaLibrary(HibernateMediaLibrary<?> hibernateMediaLibrary)
-
isRunning
public boolean isRunning()
-
setDatabaseBaseFilename
public void setDatabaseBaseFilename(File databaseBaseFilename)
Sets the database base file name. I.e. this file will not be the file the database it stored in.- Parameters:
databaseBaseFilename
- base file name- See Also:
getDatabaseBaseFilename()
,doesDatabaseExist(java.io.File)
-
getDatabaseBaseFilename
public File getDatabaseBaseFilename()
Returns the database base file name. This file does not exist!- Returns:
- base file name
- See Also:
setDatabaseBaseFilename(java.io.File)
,doesDatabaseExist(java.io.File)
-
getDatabaseBaseDirectory
public File getDatabaseBaseDirectory()
Returns the database directory name.- Returns:
- directory name
-
doesDatabaseExist
public static boolean doesDatabaseExist(File databaseBaseFilename)
Indicates whether a database actually exists. The test is file-based.- Parameters:
databaseBaseFilename
- database base filename- Returns:
- true, if the file exists
-
removeDatabaseLock
public void removeDatabaseLock()
-
executeTransaction
public <R> R executeTransaction(Function<org.hibernate.Session,R> work)
Execute some database work inside a transaction. The transaction is automatically rolled back, should an exception be thrown by the given work.- Type Parameters:
R
- result type- Parameters:
work
- stuff to do inside the transaction.- Returns:
- result
- See Also:
executeTransaction(Consumer)
-
executeTransaction
public void executeTransaction(Consumer<org.hibernate.Session> work)
Execute some database work inside a transaction. The transaction is automatically rolled back, should an exception be thrown by the given work.- Parameters:
work
- stuff to do inside the transaction.- See Also:
executeTransaction(Function)
-
execute
public <R> R execute(Function<org.hibernate.Session,R> work, boolean failSilently)
Execute some database work with a given session. The session is thread-local and will be closed after the work is done, unless a transaction is alreadyTransactionStatus.ACTIVE
before the work is started.- Type Parameters:
R
- result type- Parameters:
work
- stuff to do with the sessionfailSilently
- do not report failure, simply returnnull
- Returns:
- result
- See Also:
executeTransaction(Function)
-
execute
public <R> R execute(Function<org.hibernate.Session,R> work)
Execute some database work with a given session. The session is thread-local and will be closed after the work is done, unless a transaction is alreadyTransactionStatus.ACTIVE
before the work is started.- Type Parameters:
R
- result type- Parameters:
work
- stuff to do with the session- Returns:
- result
- See Also:
executeTransaction(Function)
-
enableFastImportSettings
public void enableFastImportSettings(boolean enable)
Sets special database settings for the very first import, i.e. when the database is empty. See H2 docs for details.- Parameters:
enable
-true
orfalse
-
openSession
@Deprecated public org.hibernate.Session openSession()
Deprecated.Use this for queries that have results that never leak to other threads. Useexecute(Function)
, if possible.- Returns:
- session
- See Also:
closeSession()
,execute(Function)
-
closeSession
@Deprecated public void closeSession()
Deprecated.Close session for the current thread. Useexecute(Function)
, if possible.- See Also:
execute(Function)
-
init
public void init()
-
handleException
public void handleException(org.hibernate.JDBCException e)
-
handleException
public void handleException(SQLException sqlException)
-
markDatabaseForDeletion
public void markDatabaseForDeletion() throws IOException
- Throws:
IOException
-
markDatabaseForRecovery
public void markDatabaseForRecovery() throws IOException
- Throws:
IOException
-
dumpLocks
public static void dumpLocks(Connection connection)
-
shutdown
public void shutdown()
-
shutdown
public void shutdown(boolean compact)
-
logStatistics
public void logStatistics()
Log Hibernate statistics, if enabled.
-
getBestIndex
public static String getBestIndex(Class<?> entity, String... orderedColumns)
Extracts the best index for the given entity class for the given columns from JPA class annotations. The returned index may be used in SQLUSE INDEX
hints.- Parameters:
entity
- entityorderedColumns
- ordered columns- Returns:
- an index or
null
, if we cannot find a suitable index
-
-