在 GlassFish 上自动启动 JavaDB (Derby)

发布于 2024-09-14 14:59:40 字数 264 浏览 6 评论 0原文

我计划将 GlassFish v3 开源版本部署到生产环境。它附带了 JavaDB (Apache Derby),这正是我所需要的。唯一的问题是 GlassFish 启动时默认情况下不会启动 JavaDB。我必须进入命令行并输入:

asadmin start-database

有没有办法让数据库在服务器(GlassFish)启动时自动启动?我讨厌在开发应用程序时每次都手动执行此操作,并且我当然不想在生产中执行此操作。

提前致谢

I am planning to deploy GlassFish v3 open source edition to a production environment. It comes with JavaDB (Apache Derby) which is just what I need. The only problem is that JavaDB is not started by default when GlassFish starts. I would have to go to the command line and enter:

asadmin start-database

Is there a way to make the database start automatically whenever the server (GlassFish) starts? I hated doing that manually everytime while I was developing my application and I certainly don't want to do that in production.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

似最初 2024-09-21 14:59:40

这有点过时,但 Eclipse 首选项的 GlassFish 部分中有一个复选框,标题为“启动 GlassFish Server 时启动 JavaDB 数据库进程”。我正在运行 Eclipse Indigo SR1。

This is a little dated but there's a checkbox in the GlassFish section in Eclipse's preferences titled "Start the JavaDB database process when starting GlassFish Server". I am running Eclipse Indigo SR1.

巷雨优美回忆 2024-09-21 14:59:40

一旦投入生产,您可以启动数据库一次并使其保持运行,无论应用程序服务器的状态如何。

您可以创建一个 shell 脚本,将启动域和启动数据库“捆绑”到单个 uber-start 命令中。

Once you go into production, you can start the db once and just leave it running, regardless of the state of the app server.

You could create a shell script to 'bundle' start-domain and start-database into a single uber-start command.

孤城病女 2024-09-21 14:59:40

这就是我所做的,我将其部署到打包为 EJB JAR 的服务器上。这将使 Derby 服务器能够作为其自己的企业应用程序启动。

import java.io.PrintWriter;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

import org.apache.derby.impl.drda.NetworkServerControlImpl;

@Singleton
@Startup
public class LocalDatabase {

private NetworkServerControlImpl networkServerControlImpl = null;

@PostConstruct
private void init() throws Exception {
    networkServerControlImpl = new NetworkServerControlImpl();
    networkServerControlImpl.start(new PrintWriter(System.out));
}

}

This is what I do, I deploy this to the server packaged as an EJB JAR. This will enable the Derby server to be started as its own enterprise application.

import java.io.PrintWriter;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

import org.apache.derby.impl.drda.NetworkServerControlImpl;

@Singleton
@Startup
public class LocalDatabase {

private NetworkServerControlImpl networkServerControlImpl = null;

@PostConstruct
private void init() throws Exception {
    networkServerControlImpl = new NetworkServerControlImpl();
    networkServerControlImpl.start(new PrintWriter(System.out));
}

}
浅唱々樱花落 2024-09-21 14:59:40

我建议利用以下事实:Derby 既可以作为嵌入式服务器(即在应用程序服务器 JVM 中运行)运行,也可以作为网络服务器(即为发送到以下地址的客户端请求提供服务):来自本地主机的默认端口 1527)。因此,您可以从嵌入式模式的增强性能中受益,但仍然允许在服务器运行时从“ij”等进行访问以管理数据,以及通过具有适当安全设置的 TCP/IP 从其他服务器实例进行访问。
在该配置中,Derby 与应用程序服务器一起启动和停止。无需额外的命令或显式的服务器启动代码即可启动 derby。

下面描述了 Glassfish 4 和 derby/javaDB 10.10 的配置,但在其他服务器和版本中的工作方式类似。您确实必须调整下面的所有路径以匹配您自己的安装。

.1.通过添加 derby.jar、derbyclient.jar、derbynet.jar、derbytools.jar,使 derby 类可用于服务器“通用”类加载器。例如,将 jar 复制到服务器实例的 JVM lib/ext 中,例如复制到 C:\java\J2EESDK7U1\glassfish\domains\domain1\lib\ext

.2。使用 glassfish 管理 GUI,将以下两个属性添加到 Configurations >服务器配置> JVM设置> JVM 选项选项卡:
-Dderby.drda.startNetworkServer=true
-Dderby.system.home=C:/java/J2EESDK7U1/glassfish/databases。第一个告诉 Derby 在加载嵌入式引擎时开始以网络模式侦听,第二个提供 derby 数据库的基本路径和可选的 derby.properties 文件(例如,使用 PROD 中的安全设置)

0.3。安排服务器在启动时加载 org.apache.derby.jdbc.EmbeddedDriver 类。实现此目的的一种方法是使用 @Startup 注解 EJB,然后在 EJB 中定义 @PostConstruct 注解方法,类似的:

@PostConstruct
private void startup() {
  try { Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
      logger.info("Started DERBY in embedded+network mode");
  } catch (ClassNotFoundException e) {
      ... your error handling
  }
}

对于 6 个其他启动技巧,请参阅http://blog.eisele。 net/2010/12/seven-ways-to-get-things-started-java.html

I would advise to take advantage from the fact that Derby can perform both as embedded server (i.e. running in the app server JVM) and network server (i.e. servicing client requests addressed to default port 1527 from the local host). Therefore you benefit from the increased performances of the embedded mode, yet still allow access from e.g. "ij" to administer data while the server is running, and from other server instances over TCP/IP with suitable security settings.
In that configuration, Derby starts and stops along with the application server. No need for extra commands or explicit server start code to launch derby.

The configuration is described below for Glassfish 4 and derby/javaDB 10.10, but will work similarly in other servers and versions. You have indeed to adjust all paths below to match with your own installation.

.1. Make derby classes available to the server 'common' class loader by adding derby.jar, derbyclient.jar, derbynet.jar, derbytools.jar. Copy the jar's for instance into the JVM lib/ext of your server instance, e.g. into C:\java\J2EESDK7U1\glassfish\domains\domain1\lib\ext

.2. Using the glassfish admin GUI, add the following two properties to Configurations > server-config > JVM settings > JVM Options tab:
-Dderby.drda.startNetworkServer=true and
-Dderby.system.home=C:/java/J2EESDK7U1/glassfish/databases. The first tells Derby to start listening in network mode when the embedded engine is loaded, the second supplies the essential path to your derby databases and the optional derby.properties file (e.g. with your security settings in PROD)

.3. arrange for the server to load the class org.apache.derby.jdbc.EmbeddedDriver at startup. A way to achieve this is for instance to annotate an EJB with @Startup, and then define a @PostConstruct annotated method in the EJB, alike:

@PostConstruct
private void startup() {
  try { Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
      logger.info("Started DERBY in embedded+network mode");
  } catch (ClassNotFoundException e) {
      ... your error handling
  }
}

for 6 other startup tips, see http://blog.eisele.net/2010/12/seven-ways-to-get-things-started-java.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文