Suse下服务自启动 |
|
|
在/etc/init.d目录下加入start,内容如下
---------------
#!/bin/sh
JAVA_HOME=/usr/java/jdk1.5.0_17
CLASSPATH=.:$JAVA_HOME/lib/tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
/opt/apache-activemq-5.4.2/bin/activemq start
---------------
再执行
chmod +x start
chkconfig --add start
chkconfig start on
|
ThreadLocal和AtomicInteger例子代码 |
|
|
import java.util.concurrent.atomic.AtomicInteger;
public class UniqueThreadIdGenerator {
private static final AtomicInteger uniqueId = new AtomicInteger(1);
private static final ThreadLocal <Integer> uniqueNum =
new ThreadLocal <Integer> () {
@Override
protected Integer initialValue() {
return uniqueId.getAndIncrement();
}
};
public static void main(String[] args) throws Exception
{
new Thread(){
public void run()
{
setCurrentThreadId(1);
System.out.println(getCurrentThreadId());
try{
Thread.sleep(2000);
}catch(Exception e)
{}
System.out.println(getCurrentThreadId());
}
}.start();
new Thread(){
public void run()
{
setCurrentThreadId(6);
System.out.println(getCurrentThreadId());
try{
Thread.sleep(2000);
}catch(Exception e)
{}
System.out.println(getCurrentThreadId());
}
}.start();
}
public static int getCurrentThreadId() {
return (int)uniqueNum.get();
}
public static void setCurrentThreadId(int i)
{
uniqueNum.set(i);
}
}
|
linux安装jdk |
|
|
#chmod +x jdk-1_5_0_02-linux-i586.rpm.bin
#./jdk-1_5_0_02-linux-i586.rpm.bin
#chmod +x jdk-1_5_0_02-linux-i586.rpm
#rpm -ivh jdk-1_5_0_02-linux-i586.rpm
#vi /etc/profile
#set java environment
JAVA_HOME=/usr/java/jdk1.5.0_17
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
#source /etc/profile
#source /etc/profile
|
Hibernate编程式测试数据库连接 |
|
|
File file = new File("G:\\workspace\\AGRI_BSS_V2\\test\\com\\jdbc\\hibernate.cfg.xml");
Configuration cfg = new Configuration().configure(file);
SessionFactory sf = cfg.buildSessionFactory();
Session s = sf.getCurrentSession();
Transaction transaction = s.beginTransaction();
SQLQuery query = s.createSQLQuery("update tb_delivery t set t.remark='qwrerqwerew'");
query.executeUpdate();
transaction.commit();//执行此方法后,session自动关闭了,因为session的获取方法时sf.getCurrentSession();
//1.getCurrentSession创建的session会和绑定到当前线程
//2.getCurrentSession创建的session会在事务回滚或事物提交后自动关闭
sf.close();
|
JDBC直连代码 |
|
|
Connection conn = null;
try
{
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.151.251:1521:orcl", "agri", "linkage");
conn.setAutoCommit(false);
PreparedStatement ps = conn.prepareStatement("update tb_delivery t set t.remark='aaa'");
ps.executeUpdate();
ps.close();
conn.commit();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
|
JVM配置 |
|
|
-Xmx512m -Xms512m -Djava.rmi.server.hostname=localhost -Dcom.sun.management.jmxremote.port=8061 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
|