From: Roland Haeder Date: Tue, 6 Oct 2015 06:39:29 +0000 (+0200) Subject: moved from jshop-core project X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=1c49ae234e12bc815f83ed0e1159e67aab7ffef1;p=jcore-utils.git moved from jshop-core project Signed-off-by:Roland Häder --- diff --git a/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java b/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java new file mode 100644 index 0000000..c1c8e54 --- /dev/null +++ b/src/org/mxchange/jcoreee/database/BaseDatabaseBean.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcoreee.database; + +import java.io.Serializable; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import org.mxchange.jcoreeelogger.beans.local.logger.Log; +import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; + +/** + * A helper class for beans that access the database. + *

+ * @author Roland Haeder + */ +public abstract class BaseDatabaseBean implements Serializable { + + /** + * Serial number + */ + private static final long serialVersionUID = 217_687_175_985_875L; + + /** + * Entity manager + */ + @PersistenceContext + private EntityManager entityManager; + + /** + * Logger instance + */ + @Log + private LoggerBeanLocal loggerBeanLocal; + + /** + * Protected constructor + */ + protected BaseDatabaseBean () { + try { + // Get initial context + Context context = new InitialContext(); + + // Lookup logger + this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N + } catch (final NamingException ex) { + // Continue to throw + throw new RuntimeException("context.lookup() failed.", ex); //NOI18N + } + } + + /** + * Getter for connection instance + *

+ * @return Connection instance + */ + protected EntityManager getEntityManager () { + return this.entityManager; + } + + /** + * Getter for loggerBeanLocal + *

+ * @return Logger instance + */ + protected LoggerBeanLocal getLoggerBeanLocal () { + return this.loggerBeanLocal; + } +}