Chapter 2. Architecture
Table of Contents
2.1. Overview
2.1.1. Minimal architecture
2.1.2. Comprehensive architecture
2.1.3. Basic APIs
2.2. JMX Integration
2.3. Contextual sessions
2.1. Overview
站在很高的层次看Hibernate,它是下面这个样子:
由于Hibernate架构十分的灵活,所以我们不可能提供所有可能的运行时架构。这里只介绍介绍两种特殊情况:
2.1.1. Minimal architecture
最简架构:应用程序自己管理它的JDBC连接,并把这些连接提供给Hibernate。同时,应用程序自己管理transaction。这种方式使用了Hibernate API的一个最小子集。
2.1.2. Comprehensive architecture
"comprehensive"架构使应用程序从底层的JDBC和JTA API抽象出来,并让Hibernate来管理这些细节。
2.1.3. Basic APIs
SessionFactory (org.hibernate.SessionFactory)
A thread-safe, immutable不可变的 cache of compiled mappings for a single database. A factory for org.hibernate.Session instances. A client oforg.hibernate.connection.ConnectionProvider. Optionally maintains a second level cache of data that is reusable between transactions at a process or cluster 集群level.
Session (org.hibernate.Session)
A single-threaded, short-lived object representing a conversation between the application and the persistent store. Wraps a JDBC java.sql.Connection. Factory fororg.hibernate.Transaction. Maintains a first level cache of persistent the application's persistent objects and collections; this cache is used when navigating the object graph or looking up objects by identifier.
Persistent objects and collections
Short-lived, single threaded objects containing persistent state and business function. These can be ordinary JavaBeans/POJOs. They are associated with exactly one org.hibernate.Session. Once the org.hibernate.Session is closed, they will be detached and free to use in any application layer (for example, directly as data transfer objects to and from presentation). Chapter 11, Working with objects discusses transient, persistent and detached object states.
Transient and detached objects and collections
Instances of persistent classes that are not currently associated with a org.hibernate.Session. They may have been instantiated by the application and not yet persisted, or they may have been instantiated by a closed org.hibernate.Session. Chapter 11, Working with objects discusses transient, persistent and detached object states.
Transaction (org.hibernate.Transaction)
(Optional) A single-threaded, short-lived object used by the application to specify atomic units of work. It abstracts the application from the underlying JDBC, JTA or CORBA transaction. A org.hibernate.Session might span several org.hibernate.Transactions in some cases. However, transaction demarcation, either using the underlying API or org.hibernate.Transaction, is never optional.
ConnectionProvider (org.hibernate.connection.ConnectionProvider)
(Optional) A factory for, and pool of, JDBC connections. It abstracts the application from underlying javax.sql.DataSource or java.sql.DriverManager. It is not exposed to application, but it can be extended and/or implemented by the developer.
TransactionFactory (org.hibernate.TransactionFactory)
(Optional) A factory for org.hibernate.Transaction instances. It is not exposed to the application, but it can be extended and/or implemented by the developer.
Extension Interfaces
Hibernate offers a range of optional extension interfaces you can implement to customize the behavior of your persistence layer. See the API documentation for details.
2.2. JMX Integration
JMX is the J2EE standard for the management of Java components. Hibernate can be managed via a JMX standard service. AN MBean implementation is provided in the distribution: org.hibernate.jmx.HibernateService.
2.3. Contextual sessions
大多数使用Hibernate的应用程序都需要某种形式的"contextual" session,在这个给定的context的scope中,session都是有效地。然而,通常情况下,应用程序对 “由什么组成context”的定义是不同的;由于对current的理解不同,导致不同的context定义不同的scope。
使用Hibernate 3.0(及以前)的应用程序通常会利用ThreadLocal-based contextual sessions和辅助类HibernateUtil,或者是利用第三方的框架,例如Spring和Pico,他们可以提供proxy/interception-based contextual sessions.
从Hibernate 3.0.1开始,Hibernate添加了 SessionFactory.getCurrentSession()方法。起初,这个方法假定使用JTA transactions,而JTA transaction定义当前session的scope和context。考虑到很多独立的JTA TransactionManager实现都很成熟,在绝大多数情况下,应用程序应该使用JTA transaction management。不论应用程序是否部署到J2EE容器中去。基于此,the JTA-based contextual sessions are all you need to use.
但是到了Hibernate 3.1,SessionFactory.getCurrentSession()底层的处理变成了可
--- The error occurred in sqlmap/shared/EgSiteLog-sqlmap-mapping.xml. --- The error occurred while applying a parameter map. --- Check the ecmng.EgSiteLog.insert-InlineParameterMap. --- Check the statement (update failed). --- Cause: java.sql.SQLException: Incorrect string value: '\xD0\xDE\xB8\xC4\xC4\xDA...' for column 'sysaction' at row 1 at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:107) at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:393) at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82) at org.springframework.orm.ibatis.SqlMapClientTemplate$9.doInSqlMapClient(SqlMapClientTemplate.java:399) at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209) ... 85 more Caused by: java.sql.SQLException: Incorrect string value: '\xD0\xDE\xB8\xC4\xC4\xDA...' for column 'sysaction' at row 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2111) at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1362) at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:168) at net.sf.log4jdbc.PreparedStatementSpy.execute(PreparedStatementSpy.java:413) at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate(SqlExecutor.java:80) at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteUpdate(MappedStatement.java:216) at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:94) ... 89 more
刚刚碰到这个问题,是跟编码相关,在创建表的时候,会让你选择一种编码方式,我没有选择,就是用默认的(我的默认编码方式是latin1),解决的办法是将这个字段的编码方式改成gbk(注意:这个时候如果再去修改表的编码方式,对字段的编码方式貌似是没有影响的)
已有 0 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐
- —软件人才免语言低担保 赴美带薪读研!—
发现N久没写博文了,有什么笔记也直接记到evernote里了,也不知这是好习惯还是坏习惯鸟~
记得在哪看过说,perl语言精髓是达到一个目的可以有很多种方法。
这话说得挺对的,因为就光发送邮件,你也会发现TM方法太多了,还得google一下选个比较好的方法
最后选了使用MIME::Lite模块,感觉挺强大的,上手也快
代码如下
sub mail_list{ exit 99 if(!@some_rule); my ($sec,$min,$hour,$day,$month,$year,@tmp)=localtime; # 邮件加个时间戳 ($min,$hour,$day,$month,$year) = ( sprintf("%02d",$min), sprintf("%02d",$hour), sprintf("%02d",$day), sprintf("%02d",++$month), sprintf("%04d",$year+1900) ); my $from_addr = 'root@xxx.com'; #发送方 my $to_addr = 'flw521521@xxx.com'; #接收方 my $cc_addr = 'cc@xxx.com'; #抄送 my $subject = '邮件标题'; #邮件标题 my $encode = 'quoted-printable'; #模块发送邮件内容时采用的编码方式 my $data = "say hello\n"; my $msg = MIME::Lite->new( From => $from_addr, To => $to_addr, Cc => $cc_addr, Subject => $subject, Type => 'multipart/mixed' # 指定总的内容为多种,如果要发送附件的话,就是这个模式 ); $msg->attach( Type => 'text/html;charset=gb2312', #指定邮件内容编码方式 Encoding => $encode, Data => qq{ #接下来就是邮件中文了,简单的html编码,其中可以穿插perl变量 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> </head> <boby> <h5>您好!</h5> <table border="1"> <tr> <th>表格1</th><th>表格2</th><th>表格3</th> </tr> </table> <hr /> $data #穿插一个变量,很方便 <hr /> <h5 align="right">ITEYE</h5> <h5 align="right">flw521521</h5> <h5 align="right">$year-$month-$day $hour:$min</h5> </boby> </html> }, ); $msg->send; }
编码这块,有两个,一个是MIME::Lite模块发送内容时的编码,一个html的编码
html代码那块的编码head编码设定,可要可不要,保险起见,就写一下。
如果要发送其他附件的话,添加一些邮件附属信息即可
$msg->attach( //发送附件 Type => 'auto', //附件类型自动判断 Path => '/home/abc.txt', //附件的路径 Disposition => 'attachment', //配置为附件,不在正文内 ); $msg->attach( //发送图片附件,随正文 Type =>'image/gif', Path =>'/home/abc.gif', Filename =>'logo.gif', );
就这些吧,具体模块用法转至CPAN:http://search.cpan.org/~rjbs/MIME-Lite-3.029/lib/MIME/Lite.pm
已有 0 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐
- —软件人才免语言低担保 赴美带薪读研!—