Derby + Spring + Maven + Hibernate + Eclipse + Unix

1.下载Derby.zip,并且解压
[url]http://db.apache.org/derby/derby_downloads.html[/url]

2.设置环境变量
用户环境
名字:DERBY_HOME
值:F:\100-programs\db-derby-10.8.3.0-bin

系统环境
在Path加入:%DERBY_HOME%\bin

3.行网络模式的Derby数据库
这种模式下,需要使用两个控制台窗口,一个用于启动Derby数据库服务端,另一个做为访问Derby数据库的客户端。

启动Derby数据库服务端:
F:\100-programs\db-derby-10.8.3.0-bin\bin>[b]startNetworkServer[/b]

数据库就启动了,启动成功会在控制台输出如下信息:
Security manager installed using the Basic server security policy. Apache Derby Network Server - 10.4.1.3 - (648739) started and ready to accept connections on port 1527 at 2008-04-28 17:13:13.921 GMT

如果碰到Runtimeexception,可以试下10.8.3.0以下的版本

关闭Derby数据库服务端:
F:\100-programs\db-derby-10.8.3.0-bin\bin>[b]stopNetworkServer[/b]

4.在另一个控制台使用ij命令访问Derby数据库服务端
使用ij工具来进行数据库交互,执行SQL脚本,如查询、增删改、创建表等等。
F:\100-programs\db-derby-10.8.3.0-bin\bin>[b]ij[/b]
ij version 10.8.3.0
ij>

创建一个数据库:
ij>connect 'jdbc:derby://localhost:1527/myderbydb;user=APP;password=APP;create=true';

如果碰到ERROR XJ041: Failed to create database,可以试下10.10.1.1版本

连接数据库:
ij>connect 'jdbc:derby//localhost:1527/myderbydb;user=APP;password=APP';

退出连接>
ij>disconnect;

退出ij
ij>exit;

5.Spring配置
	<bean id="dataSourceDerby" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<description>derby xa datasource</description>
<property name="uniqueResourceName">
<value>jdbc/DerbyDB</value>
</property>
<property name="xaDataSourceClassName">
<value>org.apache.derby.jdbc.ClientXADataSource</value>
</property>
<property name="xaProperties">
<props>
<prop key="user">APP</prop>
<prop key="password">APP</prop>
<prop key="serverName">localhost</prop>
<prop key="portNumber">1527</prop>
<prop key="databaseName">myderbydb</prop>
</props>
</property>
</bean>

<bean id="emfDerby"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"
/> </property> -->
<property name="persistenceUnitName" value="derby" />
<property name="dataSource" ref="dataSourceDerby" />
<property name="jpaProperties">
<props>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory
</prop>
<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup
</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">${orm.showsql}</prop>

<prop key="hibernate.transaction.manager_lookup_class">com.atomikos.icatch.jta.hibernate3.TransactionManagerLookup
</prop>
<prop key="hibernate.transaction.factory_class">com.atomikos.icatch.jta.hibernate3.AtomikosJTATransactionFactory
</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.ejb.transaction.JoinableCMTTransactionFactory
</prop>
<prop key="hibernate.ejb.event.post-insert">org.hibernate.ejb.event.EJB3PostInsertEventListener
</prop>
<prop key="hibernate.ejb.event.post-update">org.hibernate.ejb.event.EJB3PostUpdateEventListener
</prop>
<prop key="hibernate.ejb.event.post-delete">org.hibernate.ejb.event.EJB3PostDeleteEventListener
</prop>
<!-- <prop key="hibernate.current_session_context_class">org.hibernate.context.ThreadLocalSessionContext -->
<!-- </prop> -->
<prop key="hibernate.current_session_context_class">org.hibernate.context.JTASessionContext
</prop>
</props>
</property>
</bean>



6.JTA配置
	<persistence-unit name="derby" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/DerbyDB</jta-data-source>
<!-- class definitions here, nothing else -->
<class>myderbydb.bom.Timerjob</class>
<properties>
<property name="hibernate.default_schema" value="app" />
</properties>
</persistence-unit>


7.Maven配置
		<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.8.3.0</version>
</dependency>


7. DAO
public class TimerjobDAO {
import javax.persistence.EntityManager;

private EntityManager entityManager;

@PersistenceContext(unitName = "derby")
public void setEntityManger(final EntityManager entityManager) {
this.entityManager = entityManager;
}
}


8.Eclipse External Tools 配置启动Derby服务器端
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramLaunchConfigurationType">
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="F:\100-programs\db-derby-10.8.3.0-bin\bin\startNetworkServer.bat"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="F:\100-programs\db-derby-10.8.3.0-bin\data"/>
</launchConfiguration>


9.Unix
9.1 下载db-derby-10.8.3.0-bin.tar.gz

9.2 解压
tar xzvf db-derby-10.8.3.0-bin.tar.gz

9.3 编写脚本derby,设置环境变量,启动Derby服务器端
由于关掉Putty, Derby Server 会自动关闭,所以要将derby脚本建立在 /etc/init.d下面
Path: /etc/init.d/derby
sudo /etc/init.d/derby start


#!/bin/bash
#
# Startup script for Derby

HOME=/home/smartbus

export JAVA_HOME=$HOME/programs/java-7
export DERBY_HOME=$HOME/programs/db-derby-10.8.3.0-bin
export PATH=$PATH:$DERBY_HOME/bin
export DERBY_INSTALL=$DERBY_HOME
export CLASSPATH=$CLASSPATH:$DERBY_INSTALL/lib/derbytools.jar:$DERBY_INSTALL/lib/derbynet.jar

DBDIR=$HOME/programs/db-derby-10.8.3.0-bin/bin

start() {
echo "DERBY instance is already running"
cd $DBDIR
nohup setNetworkServerCP
nohup startNetworkServer
}

stop() {
echo "DERBY instance is already stopping"
cd $DBDIR
stopNetworkServer
}

restart() {
stop
start
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac

exit $?


9.4 另一个控制台使用ij命令访问Derby数据库服务端
export DERBY_HOME=/home/smartbus/programs/db-derby-10.8.3.0-bin
export PATH=$PATH:$DERBY_HOME/bin
export JAVA_HOME=/home/smartbus/grams/java-7
cd $DERBY_HOME/bin
ij

10. derby.properties
derby.drda.host=0.0.0.0 修改主机IP地址,由此运行所有Client连接此Server
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值