`
zyazyz008
  • 浏览: 4034 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

hibernate session获取Connection

阅读更多
public int executeSql(String[] sql) {
		int key = 0;

		ConnectionProvider cp = ((SessionFactoryImplementor) getSession().getSessionFactory()).getConnectionProvider();
		Connection conn = null;
		PreparedStatement pps = null;
		try {
			conn = cp.getConnection();
			conn.setAutoCommit(false);
			for (int i = 0; i < sql.length; i++) {
				if (sql[i] == null || "".equals(sql[i]))
					break;
				pps = conn.prepareStatement(sql[i]);
				pps.execute();
				pps.close();
				key++;
				if (key % 1000 == 0) {
					conn.commit();					
				}
			}
			conn.commit();
		} catch (Exception e) {
			if (conn != null)
				try {
					conn.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
		}finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return key;
	}
分享到:
评论

相关推荐

    Hibernate_Session_Transaction

    与Web开发中的`HttpSession`不同,`Hibernate Session`与JDBC的`Connection`更为相似,负责处理数据的持久化操作。`Session`具有短暂的生命周期,通常在一次数据库事务的开始和结束之间创建和关闭。 `Session`具有...

    hibernate和session学习

    【hibernate与Session详解】 Hibernate 是一个强大的Java对象关系映射(ORM)框架,它为开发者提供了在Java应用程序中操作数据库的强大工具。通过Hibernate,我们可以将数据库中的表与Java类进行映射,从而实现对...

    Hibernate的Session的javadoc

    - **connection()**:获取Session与数据库的连接,用于低级别操作。 - **close()**:关闭Session,释放资源,包括关闭JDBC连接。 - **cancelQuery()**:取消当前执行的查询。 5. **线程安全**: Session不是...

    hibernate中session对象的状态详解

    hibernate中session对象的状态详解

    Hibernate_session_factory_配置.docx

    在Java的持久层框架Hibernate中,SessionFactory是核心组件之一,它是线程安全的,负责创建Session对象,每个Session对应于数据库的一次会话。配置SessionFactory主要是通过Hibernate的配置文件(通常为hibernate....

    HibernateSession , Transaction 研究

    `Session`的内部维护了一个JDBC的`Connection`对象,而`Transaction`则管理着`Connection`上的事务状态。`SessionFactory`是`Session`的工厂,负责创建和配置`Session`。`SessionFactory`一般在应用启动时创建并缓存...

    JavaEE技术-自主学习.zip_hibernate_hibernate session _javaee映射.xml

    &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/mydb &lt;property name="hibernate.connection.username"&gt;root &lt;property name="hibernate.connection.password"&gt;password &lt;!-- ...其他...

    JAVA的hibernate手动获取session的方法

    本篇将详细讲解如何在Hibernate中手动获取Session,以及涉及到的相关配置和连接管理。 首先,理解Hibernate的核心组件——Session。Session是Hibernate中的工作单元,它是与数据库交互的主要接口,负责保存、更新和...

    Hibernate_11session

    《深入理解Hibernate Session:从配置到实践》 Hibernate,作为Java领域中一款广泛使用的对象关系映射(ORM)框架,极大地简化了数据库操作。在Hibernate中,Session是连接应用程序和数据库的重要桥梁,它负责对象...

    hibernate.properties

    #hibernate.connection.provider_class org.hibernate.connection.DriverManagerConnectionProvider #hibernate.connection.provider_class org.hibernate.connection.DatasourceConnectionProvider #hibernate....

    Spring与Hibernate集成中的session

    - 合理配置`hibernate.connection.release_mode`,确保在事务结束时正确关闭`Session`。 总结,Spring与Hibernate的集成使得我们可以在Spring的控制下更好地管理`Session`,实现更高效的事务处理,同时保持代码的...

    Hibernate lazy加载FOR Connection

    标题中的“Hibernate lazy加载FOR Connection”指的是Hibernate框架中的一种特性,即懒加载(Lazy Loading)。在Hibernate中,懒加载是一种优化策略,它推迟对关联对象的加载,直到真正需要使用这些对象时才进行加载...

    hibernate于各大数据库的连接

    &lt;property name="hibernate.connection.driver_class"&gt;oracle.jdbc.driver.OracleDriver &lt;property name="hibernate.connection.url"&gt;jdbc:oracle:thin:@localhost:1521:ORCL &lt;property name="hibernate....

    Hibernate开发jar包

    Hibernate开发jar包 5.07 ... &lt;property name="connection.provider_class"&gt;org.hibernate.connection.C3P0ConnectionProvider &lt;!-- 映射配置文件 --&gt; &lt;/session-factory&gt; &lt;/hibernate-configuration&gt;

    hibernate配置参数大全

    1. **`hibernate.connection.driver_class`**:指定JDBC驱动程序的类名。例如,在PostgreSQL数据库中,这个配置项应该是`org.postgresql.Driver`。 - 示例: ```properties hibernate.connection.driver_class=...

    Hibernate3使用经验

    3. **获取 Connection**:通过 `Session` 的 `connection()` 方法获得数据库连接。 ```java Connection conn = session.connection(); PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps....

    jdbc_Hibernate总结

    其中,`Class.forName`方法虽然不直接创建Driver实例,但将类加载到JVM中,使得后续可以通过`DriverManager.getConnection`获取连接。 Hibernate是一个流行的ORM(Object-Relational Mapping)框架,它为开发者提供...

    hibernate数据库通用SQL代码

    Connection con = session.connection(); PreparedStatement pstmt = con.prepareStatement(sql); ResultSet rs = pstmt.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); Hashtable ht = null; ...

    Hibernate的配置文件

    &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/testdb &lt;property name="hibernate.connection.username"&gt;root &lt;property name="hibernate.connection.password"&gt;password ``` 2. **...

Global site tag (gtag.js) - Google Analytics