从Hibernate中获得Connection,进行Jdbc操作的注意事项和样例
2007-09-01 15:04
453 查看
从Hibernate中获得Connection,进行Jdbc操作的注意事项:
一定要进行事务处理,或hibernate事务处理或手工进行事务处理
//===============================================================================
样例一:hibernate事务处理
//===============================================================================
public void creatNewTable(String name){
String sql = "CREATE TABLE " + name + " (oid bigint NOT NULL ,flow_inc_code bigint NULL)";
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
Connection conn = null;
Statement stmt = null;
try{
tx = session.beginTransaction();
conn=session.connection();
stmt=conn.createStatement();
stmt.execute(sql);
tx.commit(); //使用 Hibernate事务处理边界
}
catch (Exception he){
tx.rollback();
}
finally{
if (conn != null) {
try {
conn.close();
}
catch(SQLException sqlex){
System.err.println(this.getClass().getName() + ".mymethod - 不能关闭数据库连接: " + sqlex.toString());
}
}
if (stmt != null) {
try {
stmt.close();
}
catch(SQLException sqlex){
System.err.println(this.getClass().getName() + ".mymethod - 不能关闭数据库连接: " + sqlex.toString());
}
}
session.close();
}
}
//===============================================================================
样例二:手工进行事务处理
//===============================================================================
public String createFormPhyTable(PubTableDef pubTableDef,
HttpServletRequest request) {
// 获得创建表单物理表sql:
String createTablesql = this
.createFormPhyTableSql(pubTableDef, request);
System.out
.println("DictionaryBOImpl.createFormPhyTable() -->> 创建表单物理表 00000 "
+ " createTablesql == " + createTablesql);
//Session session = HibernateSessionFactory.getSession();
// Transaction tx = null;
Connection conn = null;
Statement stmt = null;
try {
//tx = session.beginTransaction();
//conn=session.connection();
// conn = new JdbcUtil().getConnection();
// conn = new JdbcUtil().getJdbcConnection();
conn = new JdbcUtil().getHbmConnection();
conn.setAutoCommit(false);
System.out
.println("DictionaryBOImpl.createFormPhyTable() -->> 创建表单物理表 11111 "
+ " conn == " + conn);
java.sql.Statement statement = conn.createStatement();
statement.executeUpdate(createTablesql);// 创建表单物理表
conn.commit(); //此处一定要手工进行事务处理
statement.close();
//tx.commit(); //使用 Hibernate事务处理边界
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
// No matter what, close the session
// statement.close();
}
return createTablesql;
};
//---------------------- JdbcUtil类的getHbmConnection方法 ----------------------------
public Connection getHbmConnection() throws Exception {
conn = HibernateSessionFactory.getSession().connection();
return conn;
}
一定要进行事务处理,或hibernate事务处理或手工进行事务处理
//===============================================================================
样例一:hibernate事务处理
//===============================================================================
public void creatNewTable(String name){
String sql = "CREATE TABLE " + name + " (oid bigint NOT NULL ,flow_inc_code bigint NULL)";
Session session = HibernateSessionFactory.getSession();
Transaction tx = null;
Connection conn = null;
Statement stmt = null;
try{
tx = session.beginTransaction();
conn=session.connection();
stmt=conn.createStatement();
stmt.execute(sql);
tx.commit(); //使用 Hibernate事务处理边界
}
catch (Exception he){
tx.rollback();
}
finally{
if (conn != null) {
try {
conn.close();
}
catch(SQLException sqlex){
System.err.println(this.getClass().getName() + ".mymethod - 不能关闭数据库连接: " + sqlex.toString());
}
}
if (stmt != null) {
try {
stmt.close();
}
catch(SQLException sqlex){
System.err.println(this.getClass().getName() + ".mymethod - 不能关闭数据库连接: " + sqlex.toString());
}
}
session.close();
}
}
//===============================================================================
样例二:手工进行事务处理
//===============================================================================
public String createFormPhyTable(PubTableDef pubTableDef,
HttpServletRequest request) {
// 获得创建表单物理表sql:
String createTablesql = this
.createFormPhyTableSql(pubTableDef, request);
System.out
.println("DictionaryBOImpl.createFormPhyTable() -->> 创建表单物理表 00000 "
+ " createTablesql == " + createTablesql);
//Session session = HibernateSessionFactory.getSession();
// Transaction tx = null;
Connection conn = null;
Statement stmt = null;
try {
//tx = session.beginTransaction();
//conn=session.connection();
// conn = new JdbcUtil().getConnection();
// conn = new JdbcUtil().getJdbcConnection();
conn = new JdbcUtil().getHbmConnection();
conn.setAutoCommit(false);
System.out
.println("DictionaryBOImpl.createFormPhyTable() -->> 创建表单物理表 11111 "
+ " conn == " + conn);
java.sql.Statement statement = conn.createStatement();
statement.executeUpdate(createTablesql);// 创建表单物理表
conn.commit(); //此处一定要手工进行事务处理
statement.close();
//tx.commit(); //使用 Hibernate事务处理边界
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
// No matter what, close the session
// statement.close();
}
return createTablesql;
};
//---------------------- JdbcUtil类的getHbmConnection方法 ----------------------------
public Connection getHbmConnection() throws Exception {
conn = HibernateSessionFactory.getSession().connection();
return conn;
}
相关文章推荐
- hibernate4中使用Session doWork()方法进行jdbc操作(代码)
- Hibernate 事务与JDBC 事务同时使用注意事项
- hibernate3中如何获得到库表所有字段的名称;hibernate 与 jdbc 共存需要注意的问题
- 利用hdfs的java api进行文件操作注意事项
- jdbc的批量操作使用和注意事项
- 数据库长时间未操作自己断开org.hibernate.exception.JDBCConnectionException: could not execute query
- 企业域名更换操作系列1:为什么要进行域名更换及更换需要注意事项
- hibernate中用jdbc注意事项
- JDBC之批量操作与注意事项浅析
- EntityManager获取connection进行jdbc操作
- 在Hibernate4中,使用原生JDBC进行批操作
- centos 进行配置环境变量和需要注意的操作事项
- hibernate4 操作postgresql 注意事项
- jdbc操作数据库返回结果集的注意事项
- 顺序链表的常用操作实现及注意事项
- HDFS常用文件操作命令及注意事项
- code is 9998;desc is 插入失败exception is org.hibernate.exception.JDBCConnectionException: Could not op
- Hibernate使用Projections进行聚合操作
- 使用Apache Pig时应该注意/避免的操作或事项
- 字符串操作练习以及scanf()的注意事项