Skip to content

Commit

Permalink
#JENKINS-21058 - Reuse SessionFactory and load hibernate properties u…
Browse files Browse the repository at this point in the history
…sing hibernate prefix.
  • Loading branch information
ricardogarfe committed Jan 14, 2016
1 parent 3929d64 commit 8d659b4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
public class HibernateUtil {
private final static Logger LOGGER = Logger.getLogger(HibernateUtil.class.getName());

private static SessionFactory factory = null;

private static Configuration getConfig(final Properties extraProperties) throws HibernateException {
LOGGER.log(Level.INFO, Messages.HibernateUtil_LoadConfig());
final Configuration config = new AnnotationConfiguration().configure();
Expand All @@ -38,19 +40,20 @@ private static Configuration getConfig(final Properties extraProperties) throws
}

public static SessionFactory getSessionFactory(final Properties extraProperties) {
SessionFactory retval = null;

try {
// Load base configuration from hibernate.cfg.xml
final Configuration config = getConfig(extraProperties);
retval = config.buildSessionFactory();
} catch (final Exception e) {
// Make sure you log the exception, as it might be swallowed
LOGGER.log(Level.SEVERE, Messages.HibernateUtil_FailedSessionFactory(), e);
throw new RuntimeException(e);

if (factory == null){
try {
// Load base configuration from hibernate.cfg.xml
final Configuration config = getConfig(extraProperties);
factory = config.buildSessionFactory();
} catch (final Exception e) {
// Make sure you log the exception, as it might be swallowed
LOGGER.log(Level.SEVERE, Messages.HibernateUtil_FailedSessionFactory(), e);
throw new RuntimeException(e);
}
}
return factory;

return retval;
}

public static SessionFactory getSessionFactory() {
Expand Down Expand Up @@ -86,7 +89,7 @@ public static String getSchemaDdl(

final Configuration config = getConfig(props);
final SchemaExport generator = new SchemaExport(config);
final File tempDdlFile = File.createTempFile("jenkins_audit2db", ".ddl");
final File tempDdlFile = new File("jenkins_audit2db.ddl");

This comment has been minimized.

Copy link
@TimeInvestor

TimeInvestor Mar 7, 2016

Hi Ricardo,
What's the purpose for this change?

This comment has been minimized.

Copy link
@ricardogarfe

ricardogarfe Mar 7, 2016

Author Owner

To keep filename correctly:

  • jenkins_audit2db.dll instead of jenkins_audit2db.dl34451543251515135515155151

As a convention, is not a must. You can remove it or keep it.

This comment has been minimized.

Copy link
@TimeInvestor

TimeInvestor Mar 8, 2016

Got your idea.
Thanks. 😄

generator.setOutputFile(tempDdlFile.getPath());
generator.setFormat(true);
generator.execute(true, false, false, true);
Expand Down
14 changes: 7 additions & 7 deletions src/main/resources/hibernate.cfg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="c3p0.max_size">10</property>
<property name="c3p0.min_size">2</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.max_statements">50</property>
<property name="connection.autocommit">true</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">false</property>

<mapping
Expand Down

0 comments on commit 8d659b4

Please sign in to comment.