package com.atlassian.stash.internal.activity;

import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.springframework.beans.factory.config.AbstractFactoryBean;

/**
 * @since 3.0.7, 3.1.4, 3.2.1
 */
public class ActivityLockFactoryBean extends AbstractFactoryBean<ActivityLock> {

    private SessionFactoryImplementor sessionFactory;

    @Override
    public Class<?> getObjectType() {
        return ActivityLock.class;
    }

    public void setSessionFactory(SessionFactoryImplementor sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    protected ActivityLock createInstance() {
        return sessionFactory.getDialect() instanceof MySQLDialect ?
                new MySqlActivityLock(sessionFactory) :
                new NoOpActivityLock(); //For all other databases, don't actually lock
    }
}
