-
-
Save zoowii/5082dd171f0df2fc6386 to your computer and use it in GitHub Desktop.
将jpa-utils使用到spring mvc项目中的自定义TransactionManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.zoowii.jpa_utils.core.AbstractSession; | |
import com.zoowii.jpa_utils.core.Session; | |
import com.zoowii.jpa_utils.core.SessionFactory; | |
import com.zoowii.jpa_utils.core.impl.EntitySessionFactory; | |
import com.zoowii.jpa_utils.core.impl.JdbcSession; | |
import com.zoowii.jpa_utils.core.impl.JdbcSessionFactory; | |
import com.zoowii.jpa_utils.core.impl.JdbcTransaction; | |
import com.zoowii.jpa_utils.jdbcorm.sqlmapper.MySQLMapper; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Component; | |
import javax.persistence.EntityManagerFactory; | |
import javax.sql.DataSource; | |
import java.sql.Connection; | |
import java.sql.SQLException; | |
/** | |
* 使用这个类来操作db session | |
*/ | |
@Component | |
public class DbSession { | |
private Logger logger = LoggerFactory.getLogger(DbSession.class); | |
@Autowired | |
private DataSource dataSource; | |
// @Autowired | |
// private EntityManagerFactory entityManagerFactory; | |
private static SessionFactory sessionFactory; | |
public DbSession() { | |
} | |
private SessionFactory getSessionFactory() { | |
if(sessionFactory!=null) { | |
return sessionFactory; | |
} | |
synchronized (this) { | |
if(sessionFactory!=null) { | |
return sessionFactory; | |
} | |
sessionFactory = new JdbcSessionFactory(dataSource, new MySQLMapper()); | |
// sessionFactory = EntitySessionFactory.getSessionFactory(entityManagerFactory); | |
AbstractSession.setDefaultSessionFactory(sessionFactory); | |
return sessionFactory; | |
} | |
} | |
// 使用WeakReference会导致没有直接变量或者对象持有session的话,session会被GC,然后连接被泄露无法归还了 | |
private static final ThreadLocal<Session> defaultThreadLocalSessions = new ThreadLocal<>(); | |
public Session currentSession() { | |
try { | |
// Session session = defaultThreadLocalSessions.get(); | |
// if (session == null) { | |
// Connection conn = dataSource.getConnection(); | |
// session = new JdbcSession(conn); | |
// defaultThreadLocalSessions.set(session); | |
// } else if (session.isClosed()) { | |
// Connection conn = dataSource.getConnection(); | |
// session = new JdbcSession(conn); | |
// defaultThreadLocalSessions.set(session); | |
// } | |
getSessionFactory(); | |
Session session = AbstractSession.currentSession(); | |
if(session instanceof JdbcSession) { | |
JdbcSession jdbcSession = (JdbcSession) session; | |
jdbcSession.getJdbcConnection(); | |
jdbcSession.setAutoCommit(false); | |
} | |
AbstractSession.bindCurrentSession(session); | |
defaultThreadLocalSessions.set(session); | |
return session; | |
} catch (Exception e) { | |
logger.error("get datasource connection error", e); | |
return null; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.zoowii.jpa_utils.core.Session; | |
import org.springframework.transaction.PlatformTransactionManager; | |
import org.springframework.transaction.TransactionDefinition; | |
import org.springframework.transaction.TransactionException; | |
import org.springframework.transaction.TransactionStatus; | |
/** | |
* 自定义的jpa-utils事务管理器 | |
*/ | |
public class JpaUtilsTransactionManager implements PlatformTransactionManager { | |
private final DbSession dbSession; | |
public JpaUtilsTransactionManager(DbSession dbSession) { | |
this.dbSession = dbSession; | |
} | |
@Override | |
public TransactionStatus getTransaction(TransactionDefinition transactionDefinition) throws TransactionException { | |
Session session = dbSession.currentSession(); | |
return new TransactionStatus() { | |
@Override | |
public boolean isNewTransaction() { | |
return !session.isOpen(); | |
} | |
@Override | |
public boolean hasSavepoint() { | |
return false; | |
} | |
@Override | |
public void setRollbackOnly() { | |
} | |
@Override | |
public boolean isRollbackOnly() { | |
return false; | |
} | |
@Override | |
public void flush() { | |
session.flush(); | |
} | |
@Override | |
public boolean isCompleted() { | |
return false; | |
} | |
@Override | |
public Object createSavepoint() throws TransactionException { | |
return null; | |
} | |
@Override | |
public void rollbackToSavepoint(Object o) throws TransactionException { | |
} | |
@Override | |
public void releaseSavepoint(Object o) throws TransactionException { | |
} | |
}; | |
} | |
@Override | |
public void commit(TransactionStatus transactionStatus) throws TransactionException { | |
Session session = dbSession.currentSession(); | |
if(!session.isRunning()) { | |
session.begin(); | |
} | |
session.commit(); | |
session.close(); | |
} | |
@Override | |
public void rollback(TransactionStatus transactionStatus) throws TransactionException { | |
Session session = dbSession.currentSession(); | |
session.rollback(); | |
session.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bean id="transactionManager" class="path.to.JPAUtilsTransactionManager"> | |
</bean> | |
<tx:annotation-driven transaction-manager="transactionManager"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment