]> git.mxchange.org Git - jcore-utils.git/blob - src/org/mxchange/jcoreee/database/BaseDatabaseBean.java
The exception's message now contains the thrown exception's message, too.
[jcore-utils.git] / src / org / mxchange / jcoreee / database / BaseDatabaseBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcoreee.database;
18
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import javax.naming.Context;
22 import javax.naming.InitialContext;
23 import javax.naming.NamingException;
24 import javax.persistence.EntityManager;
25 import javax.persistence.PersistenceContext;
26 import org.mxchange.jcoreeelogger.beans.local.logger.Log;
27 import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal;
28
29 /**
30  * A helper class for beans that access the database.
31  * <p>
32  * @author Roland Haeder<roland@mxchange.org>
33  */
34 public abstract class BaseDatabaseBean implements Serializable {
35
36         /**
37          * Serial number
38          */
39         private static final long serialVersionUID = 217_687_175_985_875L;
40
41         /**
42          * Entity manager
43          */
44         @PersistenceContext
45         private EntityManager entityManager;
46
47         /**
48          * Logger instance
49          */
50         @Log
51         private LoggerBeanLocal loggerBeanLocal;
52
53         /**
54          * Protected constructor
55          */
56         protected BaseDatabaseBean () {
57                 try {
58                         // Get initial context
59                         Context context = new InitialContext();
60
61                         // Lookup logger
62                         this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N
63                 } catch (final NamingException ex) {
64                         // Continue to throw
65                         throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N
66                 }
67         }
68
69         /**
70          * Getter for connection instance
71          * <p>
72          * @return Connection instance
73          */
74         protected EntityManager getEntityManager () {
75                 return this.entityManager;
76         }
77
78         /**
79          * Getter for loggerBeanLocal
80          * <p>
81          * @return Logger instance
82          */
83         protected LoggerBeanLocal getLoggerBeanLocal () {
84                 return this.loggerBeanLocal;
85         }
86
87 }