SSH中使用getCurrentSession()获得session使用时注意事项,Hibernate,项目经验

SSH中使用getCurrentSession()获得session

 

  1. 在hibernate的配置文件中增加属性:

     

    <property name="current_session_context_class">thread</property>

    如下表红色部分

    Java代码    收藏代码
    1. <hibernate-configuration>  
    2.   
    3. <session-factory>  
    4.   
    5.    <property name="connection.username">root</property>  
    6.   
    7.    <property name="connection.url">  
    8.   
    9.     jdbc:mysql://localhost:3306/dbtest  
    10.   
    11.    </property>  
    12.   
    13.    <property name="dialect">  
    14.   
    15.     org.hibernate.dialect.MySQLDialect  
    16.   
    17.    </property>  
    18.   
    19.    <property name="myeclipse.connection.profile">dbtest</property>  
    20.   
    21.    <property name="connection.password">123456</property>  
    22.   
    23.    <property name="connection.driver_class">  
    24.   
    25.     com.mysql.jdbc.Driver  
    26.   
    27.    </property>  
    28.   
    29.    <property name="show_sql">true</property>  
    30.   
    31.   <property name="current_session_context_class">thread</property>  
    32.   
    33.    <mapping resource="com/pc386/hibernate/entity/User.hbm.xml" />  
    34.   
    35.     
    36.   
    37. </session-factory>  
    38.   
    39. </hibernate-configuration>  
    40.    

     

    在SSH中,如果把hibernate交给Spring的管理事物中,那么应该修改Spring的配置文件applicationContext.xml文件的属性,在sessionFactory的属性中增加: <prop key="current_session_context_class">thread</prop>
    Java代码    收藏代码
    1. bean id="sessionFactory"   
    2.   
    3. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
    4.   
    5. <property name="dataSource">  
    6.   
    7.    <ref local="dataSource"/>  
    8.   
    9. </property>  
    10.   
    11. <property name="mappingResources">  
    12.   
    13.    <list>  
    14.   
    15.     <value>User.hbm.xml </value>  
    16.   
    17.    </list>  
    18.   
    19. </property>  
    20.   
    21. <property name="hibernateProperties">  
    22.   
    23.    <props>  
    24.   
    25.     <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>  
    26.   
    27. <!--  
    28.   
    29.     <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>  
    30.   
    31. -->  
    32.   
    33.     <prop key="hibernate.current_session_context_class">thread</prop>  
    34.   
    35.    </props>  
    36.   
    37. </property>  
    38.   
    39. </bean>  
    40.    
     原文出处
  2. http://blog.csdn.net/daryl715/archive/2007/02/11/1507385.aspx
    Java代码    收藏代码
    1. No CurrentSessionContext configured!" 异常解决方案  
    2.   
    3. hibernate 老说没有配方言  
    4.   
    5. <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
    6.   
    7. 这句话明明就写在了配置文件里面,可老是没有写  
    8.   
    9. 错误如下:  
    10.   
    11. Exception in thread "main" org.hibernate.HibernateException: Hibernate Dialect must be explicitly set  
    12.   
    13. name改成"hibernate.dialect"也不行  
    14.   
    15. Hibernate-Version: 3.1.1  
    16.   
    17. mysql -Verison: 4.0.16  
    18.   
    19. 用这种方法正确  
    20.   
    21. static {  
    22.   
    23. try {  
    24.   
    25. configuration = new Configuration();  
    26.   
    27. sessionFactory = configuration.configure().buildSessionFactory();  
    28.   
    29. catch (Throwable ex) {  
    30.   
    31. throw new ExceptionInInitializerError(ex);  
    32.   
    33. }  
    34.   
    35. }  
    36.   
    37. 这种方式会出现异常  
    38.   
    39. // static {  
    40.   
    41. // try {  
    42.   
    43. // // Create a configuration based on the properties file we've put  
    44.   
    45. // Configuration config = new Configuration();  
    46.   
    47. // config.addClass(Customer.class).addClass(Order.class);  
    48.   
    49. // // Get the session factory we can use for persistence  
    50.   
    51. // sessionFactory = config.buildSessionFactory();  
    52.   
    53. // } catch (Exception e) {  
    54.   
    55. // e.printStackTrace();  
    56.   
    57. // }  
    58.   
    59. // }  
    60.   
    61. 第二种方式是针对使用properties文件配置hiernate的写法,使用hibernate.cfg.xml应使用第一种调用方式  
    62.   
    63. 或者在hibernate.cfg.xml中加入:  
    64.   
    65. <property name="current_session_context_class">thread</property>  
     http://blog.sina.com.cn/s/blog_412d58ec010005p5.html
    Java代码    收藏代码
    1. 以前单独用Hibernate2.0的时候,为了保证一个线程中每次取出的session都是一个对象,就使用官方提供的一个HibernateUtils,将第一次取出的session放入ThreadLocal中,以后每次从这里面取出的session都是一个对象,可以保证事务的正常执行。  
    2.   
    3. 后来升级到3.0,也这样延用下去,没怎么关心3.0的新特性。  
    4.   
    5. 前几天想将Hibernate加入到SPRING的事务管理中,但是我又不想使用spring提供的HibernateDaoSupport或HibernateTemplate,只想像以前那样在一个线程中的任意地方都能得到同一个session。这样就出问题了,spring管理事务的话,如果要保证当前线程内只有一个session,需要将sessionFactory传递给org.springframework.orm.hibernate3.HibernateTransactionManager,spring负责事务的开始,提交,回滚以及session的关闭,假设spring用于管理事务的session是(session1)。如果我还用HibernateUtils.getCurrentSession()方法获得session的话,得到的session却是(session2),和开始事务的session不是同一个对象,就造成session2的事务没有提交,对数据库的操作无效。  
    6.   
    7. 后来搜索到原来Hibernate3.0以后,sessionFactory多了个新的方法getCurrentSession()。我心里很高兴,这样不就解决我的问题了吗?  
    8.   
    9. 于是我就将HibernateUtils中的session=sessionFactory.openSession()方法改为了session=sessionFactory.getCurrentSession().  
    10.   
    11. 运行测试竟然错了,  
    12.   
    13. 提示什么TransactionManager出错了,上网上搜了搜,原来使用getCurrent方法必须配置事务管理,需要和jta或thread绑定。我不想用jta,当然要和线程绑定了。我在hibernate的配置文件中加入了<property name="current_session_context_class">thread</property>,又运行,还是出同样的错误。继续上网上查资料,基本没有相关资料。后来费了好大劲才发现,原来在hibernate3.0里只能和jta绑定,我copy了新的3.2的包,这下可以了。不出那个事务管理的错了,变成了org.hibernate.HibernateException: save is not valid without active transaction错误。继续搜,只有一个相关资料,说使用getCurrentSession()方法必须在事务中运行。不管查询还是更新操作都要开始事务,操作,提交事务才行。  
    14.   
    15. 可是我已经配置了让spring管理事务,为什么出这个错误?看来只有一个原因,spring在通过sessionFactory获取session时,没有调用getCurrentSession()方法。  
    16.   
    17. 自己手动加入事务的开始,提交,成功了。  
    18.   
    19. 经过多次实验,发现,只能能spring中配置sessionFactory,才能让spring管理hibernate的事务。  
    20.   
    21. <bean id="sessionFactory"  
    22.   
    23. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
    24.   
    25. <property name="dataSource">  
    26.   
    27.    <ref local="dataSource"/>  
    28.   
    29. </property>  
    30.   
    31. <property name="mappingResources">  
    32.   
    33.    <list>  
    34.   
    35.     <value>User.hbm.xml </value>  
    36.   
    37.    </list>  
    38.   
    39. </property>  
    40.   
    41. <property name="hibernateProperties">  
    42.   
    43.    <props>  
    44.   
    45.     <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>  
    46.   
    47. <!--  
    48.   
    49.     <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>  
    50.   
    51. -->  
    52.   
    53.     <prop key="hibernate.current_session_context_class">thread</prop>  
    54.   
    55.    </props>  
    56.   
    57. </property>  
    58.   
    59. </bean>  
    60.   
    61. 在程序中要用到session的时候,过程如下 :  
    62.   
    63. UserDAOImpl:  
    64.   
    65. {  
    66.   
    67. SessionFactory sf = [从spring中得到sessionFactory];  
    68.   
    69. Session session = sf.getCurrentSession();  
    70.   
    71. //数据库操作  
    72.   
    73. }  
    74.   
    75. 这么乱。写一下总结。  
    76.   
    77. 1. 如果想让spring帮你管理事务,只能在spring中配置SessionFactory。如果使用hibernate原有的sessionFactory,则只能自己手动管理事务。  
    78.   
    79. 2. 如果想使用sessionFactory.getCurrentSession()方法,必须配置sessionFactory和jta或thread绑定。但是hibernate3.0不支持与thread绑定,3.1以上才支持。  
    80.   
    81. 3. sessionFactory.getCurrentSession()方法取得的session,在做数据库操作时必须在事务中做,包括只读的查询,否则会报错。  
    82.    
     

猜你喜欢

转载自software-king.iteye.com/blog/2092335